I'm wondering if there is anything like this?
I'm getting my CMS to sort the content by date, so this is my approach.
First getting the current time.
var date = Date.now();
It will return me 1374426602321.
So now when I want to display that to the user and obviously we don't want to display the above, so I'm using this right now.
var formattedDate = new Date(date).toDateString(),
That will output
Sun Jul 21 2013
Which is good, but that is not really the correct or perfect way I guess.
I'm expecting more like Sunday July 21, 2013.
Is there a way that we can accomplish this by not using any of the plugin or any module and not having to create like another function to format this??
This is being done in the server-side using Node.js
use a library or a function... don't see any other way.
function getDateString(d){
return
["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"][d.getDay()]+" "+
["January","February","March","April","May","June","July","August","September","October","November","December"][d.getMonth()]+" "+
d.getDate()+", "+
d.getFullYear();
}
Related
i am trying to convert milliseconds to my desired date format. But the format that i give to the function doesn`t seem to work.
In my map function
var date = new Date(item.showTime);
var dateString = date.toString('MM/dd/yy HH:mm:ss');
emit([doc.vehicleNumber,doc.advertId],{"seatNumber":item.seatNumber,"showTime":dateString ,"skipTime":item.skipTime});
});
The result is
{seatNumber: 2, showTime: "Tue Nov 21 2017 10:08:56 GMT+0000 (UTC)", skipTime: 0}
I need show time to in format of 10/12/2017 10:08:56.. I don`t know why this is not working.
Btw this is not javascript, i think it is about couchdb so please do
not mark this as duplicate with other JS questions.
CouchDB supports the use of CommonJS Modules in the map function definition.
http://docs.couchdb.org/en/2.1.1/query-server/javascript.html#commonjs
The problem is that modules should be defined in the design document and can not be loaded from external resources.
You can use standard JavaScript built-in objects and functions in your map function as couchjs is based in Mozilla's SpiderMonkey JS interpreter. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date
There is not base support in CouchDB JS runtime for date formating. You should write your own logic for this purpose.
If this is a big issue for you, you may try to hack the /path/to/couchdb/share/server/main.js file which is the one that sets the execution contexts of your functions, but I don't see it too much recommendable.
Parse the date and format it yourself. It's not difficult.
var date = new Date(Date.parse(item.showTime));
var timestring = "" + date.getMonth() + "/" + date.getDate() + "/" +
date.getFullYear() + " " + date.toTimeString().substr(0,8)
This is very much about JavaScript. The .toString() method does not take any formatting parameters. You can write your own function to convert the output, or you can use a library that does what you want.
For example if I do for the above date object something like: value.hours(), I get as output 16 instead of 18. I believe it returns the hours in the original GMT time, not like in my date object which is GMT+2. I can of course add 2 to the returned result, but it becomes cumbersome. Is there any way to get the hours correctly in my case?
I'm not sure as to what you've already tried, but I put the following into JSFiddle and it worked like a charm. I am currently in CST in America and it is 8:30 in the morning here. When I ran the snippet below I got today's date at 1:30 PM which I would assume is accurate in difference.
HTML
<div id="m1"></div>
JavaScript
var a = moment.tz(new Date(), "GMT");
document.getElementById('m1').innerHTML = a.format("YYYY MM DD; HH:mm");
The Moment.js documentation states the following in regards to creating a Moment object with a native JavaScript Date object:
You can create a Moment with a pre-existing native JavaScript Date object.
var day = new Date(2011, 9, 16);
var dayWrapper = moment(day);
This clones the Date object; further changes to the Date won't affect the Moment, and vice-versa.
To find the information quoted above quickly, when you reach the Moment.js documentation, it is located under the Parse section under sub-section Date.
To display local time:
value.local();
value.hours(); // 18
To reverse:
value.utc();
value.hours(); // 16
I think that you can solve it by doing what the docs says. Something like this:
moment().tz("America/Los_Angeles").format();
https://momentjs.com/timezone/docs/#/using-timezones/
I know JavaScript Date objects contain getTimezoneOffset() for getting the offset, but is there any method that returns the label or name for that timezone?
For example, on my computer, in Chrome's console, if I do:
> new Date().toString();
Then I get:
< "Thu Feb 25 2016 08:49:56 GMT-0800 (Pacific Standard Time)"
What I'd like to do is take a Date and get back "Pacific Standard Time" portion for it.
Is this possible, and if so, how?
I dont think there is a reliable way without regex matching (see #nils answer). Not sure what caveats that date string comes with, so might be fragile? It looks like there are some libraries available that can simplify this
https://bitbucket.org/pellepim/jstimezonedetect/wiki/Home
var timezone = jstz.determine();
timezone.name();
"Europe/Berlin"
There's no straight forward way. You can get it through the following method.
Alternatively you can choose REGEX.
function getTimeZoneLabel(){
var str = new Date().toString();
return str.substring(str.indexOf("(")+1,str.indexOf(")"));
}
You could use a regular expression to get it:
var d = new Date().toString();
var timezone = d.match(/\(([^)]+)\)/)[1];
An approach would be to just get what's inside the paranteses. Juan Mendes posted a solution:
function getTimeZone() {
return /\((.*)\)/.exec(new Date().toString())[1];
}
getTimeZone();
Note that this is language, OS and browser specific (and therefor of course not the ideal solution).
If it is however okay for you to use a library, you could use jsTimezoneDetect.
I'm using http://momentjs.com and i don't know how to change the locale.
For example :
var shortDay = moment(myDate).format('ddd');
This line return, depend of the days, Sun Mon ... Fri Sat. I want to return the same, but in french.
I tried var shortDay = moment(myDate).locale('fr').format('ddd'); but no effect. Any ideas ?
You need to load the relevant locale file
See http://momentjs.com/docs/#/i18n/loading-into-browser/
Then you just need to call moment().locale('fr') once and it will start using french.
Ok, so I'm writing a Titanium app that digests a JSON feed and adds events to the users calendar. The problem I'm facing is that the JSON feed will have the datatime in local time (including daylight savings where appropriate), and the calendar needs UTC.
One simplification is the event is always in the same location (London), so the only real issue is allowing for DST (BST). So if I'm getting a date string like 2014-04-27 19:00:00 from the feed, what's the best way to get that into a javascript date object in UTC (which I think, in this case, would look like 2014-04-27T18:00:00Z, but it all depends on the time of year).
If you're okay with bringing in a library, Momentjs with the Timezone plugin are able to do this...
var input = '2014-04-27 19:00:00';
var eventDateInUtc = moment.tz(input, 'Europe/London').utc();
var output = eventDateInUtc.toDate().toISOString(); // '2014-04-27T18:00:00.000Z'
JSFiddle
Try this hope it will help you
var newDate = new Date();
var newDateUTC = new Date(newDate.getUTCFullYear(), newDate.getUTCMonth(), newDate.getUTCDate(), newDate.getUTCHours(), newDate.getUTCMinutes(), newDate.getUTCSeconds());