I'm migrating my code from MomentJS to date-fns and having the following issue when setting the hour and minutes.
This is my momentJS that works just fine:
var someDate = moment.utc('2020-07-16T16:35:39.955873Z')) // 2020-07-16T16:35:39.955Z
console.log(someDate.format('MM/DD/YYYY [ at ] LT ')); // 07/16/2020 at 4:35 PM
This is my code using date-fns:
var someTime = zonedTimeToUtc('2020-07-16T16:35:39.955873Z', 'utc'); // 2020-07-16T16:35:39.955Z
console.log(format(new Date(someTime), "MM/dd/yyyy 'at' h:mm a")); // 07/16/2020 at 10:35 AM
so, I want my date-fns code to print
07/16/2020 at 4:35 PM
but it's printing
07/16/2020 at 10:35 AM
Why is that? A simple way to get it to print the date that I want is by removing the "Z" from the value of someTime variable (like this: 2020-07-16T16:35:39.955), then it works, but I don't want to remove it manually. Can anyone tell me what I'm missing or doing wrong? Thanks a lot in advance!
Here's a LIVE DEMO
Try using utcToZonedTime()
To change the displayed date/time returned from format(), you must use either:
utcToZonedTime: when you want to know what the local date is in another timezone
zonedTimeToUtc: when you want to know what a date in another timezone is in the local timezone
Working Demo
Related
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 need to set a datetime-local picker's default value to the current local time. Native JS seems to output in local time by default:
new Date($.now()); // "Sat Nov 12 2016 22:36:52 GMT+1100 (AEDT)"
However functions like toISOString() output in UTC, and although I can pull out individual components locally, I don't really want to fiddle around with padding and such. So I try this using moment.js:
moment().local().format(); // "2016-11-12T22:34:05+11:00"
Cool! Now I just need to adjust the format to a tiny bit:
moment().local().format('YYYY-MM-DThh:mm'); // "2016-11-12T10:39"
Waaaaaaait. Now it's in UTC again, even though I specified local.
In this particular case I could use string manipulation to just drop the end off for the date-time picker, but surely I'm going to reach a point where I want to output the local time in an arbitrary format. Am I missing something here?
Your second example isn't UTC, it's just using 12h format.
hh = 12h, HH = 24h. Try this instead:
moment().local().format('YYYY-MM-DTHH:mm')
There is the following code:
console.log(order.start_time);
console.log(moment(order.start_time).format("HH:MM"));
I just want to get hour and minute from date using moment and display it. Output:
2014-06-30T09:00:00.000Z
13:06
But I don't understand why my date is formatted incorrectly. Thanks in advance.
Please note that there is a difference between MM and mm. The first formatting option is used for months, while the second is for days.
More information here: http://momentjs.com/docs/#/parsing/string-format/
From the doc here:
http://momentjs.com/docs/#/displaying/format/
You should use HH:mm. Then your time will have right format.
Further information, the time might be incorrectly as you expected because timezone. Momentjs auto display time in your system timezone.
Update:
To display time in specified Timezone, use "zone" method:
console.log(moment(order.start_time).zone('+00').format("HH:mm"));
i am working on javascript. and i have time in milliseconds. so i have to display the time in PST time Zone. i have tried for GMT. but is there any option to display the time in PST format in which the input is in milliseconds like 1671673550214. so iam trying to convert this milliseconds to time and date format of PST. same as the below code does. pleas help.
Here is my tried code:
var time = new Date().getTime();
var date = new Date(time);
document.write(date.toString());
i have even tried of using UTCString(), UTC() and toString(); but things is getting converted to GMT and not to PST.
Please check this library, it has lot of functions to display and manupilate date time.
http://momentjs.com/
http://momentjs.com/docs/
Also please check following post
Moment.js: Format date in a specific timezone\
If you dont want to use external lib, see the following article
http://www.techrepublic.com/article/convert-the-local-time-to-another-time-zone-with-this-javascript/6016329
You can use timezonjs which help you to convert the time to any timezone.
https://github.com/mde/timezone-js
I want to get the time difference between saved time and current time in javascript or jquery. My saved time looks like Sun Oct 24 15:55:56 GMT+05:30 2010.
The date format code in java looks like
String newDate = "2010/10/24 15:55:56";
DateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = format.parse(newDate);
How to compare it with the current time and get the difference?
Is there any inbuilt function in jquery or javascript??
Any suggestions or links would be appreciative!!!
Thanks in Advance!
Update
Date is stored as varchar in the DB. I am retriving it to a String variable and then change it to java.util.Date object. The java code looks like
String newDate = "2010/10/24 15:55:56";
DateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = format.parse(newDate);
This date object was sent to client. There i want to compare the saved date with current date and want to show the time difference like 2 secs ago, 2 hours ago, 2 days ago etc... like exactly in facebook. I have gone through some date to timestamp conversion tutorial in java script and now i can get the difference in timestamp. Now, i want to know how i shall change it to some format like "2 secs or 2 days or 24 hours"??. Or, how i shall change it back to date format???
Convert them into timestamps which are actually integers and can get subtracted from each other. The you just have to convert back the resulting timestamp to a javascript date object.
var diff = new Date();
diff.setTime( time2.getTime()-time1.getTime() );
You dont need to explicit convert, just do this:
var timediff = new Date() - savedTime;
This will return the difference in milliseconds.
jQuery doesn't add anything for working with dates. I'd recommend using Datejs in the event that the standard JavaScript Date API isn't sufficient.
Perhaps you could clarify exactly what input and output you're aiming for. What do you mean by "the difference?" There is more than one way to express the difference between to instants in time (primarily units and output string formatting).
Edit: since you said you're working with jQuery, how about using CuteTime? (Demo page)