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"));
Related
I'm trying to generate links to Google calendar and see this tool:
https://decomaan.github.io/google-calendar-link-generator/
And the links are generated as:
https://www.google.com/calendar/render?action=TEMPLATE&text=Appointment+with+VULKOVICH%2C+BILL&details=a+Description&location=a+Location&dates=20210105T103300Z%2F20210114T103300Z
and as you can see the dates are like:
20210105T103300Z
and I am trying to convert this to my own dates but I don't know which type is this and how to format. I have the dates both, in moment or in date, but don't know how to convert.
That's ISO-8601
The first part is the date in year-month-date order, the second part is the time and the final letter indicates the timezone (here Z for 'Zulu')
Since you're using moment.js: moment().utc().format('YYYYMMDDTHHmmss[Z]'), or without a library new Date().toISOString().replace(/\W/g,'').replace(/\d{3}Z/,'Z'). This is really a duplicate of How to format a JavaScript date.
Source: comment by RobG Jan 11 at 12:58
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
In my javascript the library Moment.js rounds my dates up.
Date: 2015-02-09T23:00:00.000Z
moment(Date).format('DD/MM'); ==> Becomes 10/02
I want 09/02 as result. Is there a possible way that the library not rounds the date?
The problem is likely one of timezones: by default, momentjs parses your string and converts it to your local timezone. If I see this correctly, the 'Z' in your date signifies zulu - or UTC time. If your timezone is +02:00 for example, that would make it the 10th, 01:00.
Use Moment#utc
moment(Date).utc().format('DD/MM');
to output format the date as UTC again.
Moment.js will output dates in the local time zone, so it might very well be that it's caused by a difference in timezones.
If you want to show the date/time as encoded into the original string, use parseZone like this:
var dateStr = "2015-02-09T23:00:00.000Z";
moment.parseZone(dateStr).format('DD/MM');
You can try like below.
moment(Date,['YYYY-MM-DD']).format('DD/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 am using one jquery date picker,
with using picker i am getting date in like this format
Friday, May 21, 2010
Now i want to add one day in this date so i think, i can only do if i change the date in exact format like
21/5/2010
I want to only convert that bcz i want to add one day to the particular date.
So what do u suggest me? How can I do that?
Can i do without converting it ?
thanks in advance....
Take a look at http://docs.jquery.com/UI/Datepicker#option-dateFormat
datejs may be useful to you.
In addition to the formatting options given by others, you should add using date objects rather than to the string representation of the date object.
I.E.
// add 5 days to today
var myDate=new Date();
myDate.setDate(myDate.getDate()+5);
I think you need to detail what jQuery plugin do you use.
Is it this one? http://jqueryui.com/demos/datepicker/
If so, then when you cann getDate method, you'll get Date object representing a date. You can easily manipulate it.
The date format has nothing to do with how dates are stored; it only affects the way dates are displayed. JavaScript has a native Date object and jQuery UI's Datepicker allows to access such object:
http://jqueryui.com/demos/datepicker/#method-getDate
Once you have a Date object, you can alter it to suit your needs:
https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Date
Finally, you can feed it back into Datepicker:
http://jqueryui.com/demos/datepicker/#method-setDate