The backend API I am working with only understands timezone in +0000 format.
I currently I get a date in the current format:
"2017-12-20T16:39:31.000Z"
With moment.js how would I get it in the following string format?
2017-12-20T16:39:31+0000
So far I have done:
var date = new Date();
this.lastCheckedDate = moment(date).toISOString();
You can use the .format() method to format the date in your own way:
moment("2017-12-20T16:39:31.000Z").format('YYYY-MM-DDTHH:mm:ssZZ');
If the date is in another timezone, I recommend using .utc() first to convert the timezone to UTC, and then format the date:
moment("2017-12-20T16:39:31.000+0530").utc().format('YYYY-MM-DDTHH:mm:ssZZ');
Related
How to convert a datetime to another timezone datetime then to utc using moment.js?
This has to be done without using the timezone library so functions like tz and setTimezone are not available. The datetime is entered by the user.
So '5/24/2019 20:35' in Hawaii time is expected to be 5/25/2019 06:35AM in utc.
My local computer is Pacific.
I tried this:
moment.utc(moment('5/24/2019 20:35','M/D/YYYY h:mm').utcOffset(-10).format("MM/DD/YYYY HH:mm:ss")).format('MM/DD/YYYY HH:mm:ss');
but it's not correct.
Why do you need to convert to another timezone before converting to UTC?
Just do new Date("5/24/2019 20:35 -10").toUTCString() in pure JS.
On latest chrome it gives me the correct output "Sat, 25 May 2019 06:35:00 GMT"
If you need ISO format, do new Date(datestring).toISOString() //"2019-05-25T06:35:00.000Z"
Doing this with the date-functions.js library (used e.g. in datetimepicker jQuery plugin):
Date.parseDate('2018-03-10 12:12', 'Y-m-d H:i')
gives:
Sat Mar 10 2018 12:12:00 GMT+0100 (Paris, Madrid)
How to get the result as Unix timestamp or GMT / UTC time instead?
A string like '2018-03-10 12:12' will usually be parsed as local as there is no timezone offset. It's also not ISO 8601 compliant so using the built-in parser will yield different results in different browsers.
While you can use a library, to parse it as UTC and get the time value is just 2 lines of code:
function toUTCTimeValue(s) {
var b = s.split(/\D/);
return Date.UTC(b[0],b[1]-1,b[2],b[3],b[4]);
}
// As time value
console.log(toUTCTimeValue('2018-03-10 12:12'));
// Convert to Date object and print as timestamp
console.log(new Date(toUTCTimeValue('2018-03-10 12:12')).toISOString());
var date = new Date('2018-03-10 12:12'.replace(' ', 'T'));
// Unix
console.log(Math.floor(date.getTime() / 1000));
// UTC
console.log(date.toUTCString());
As always, please have a look at the documentation at MDN: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date
Use MomentJS instead. You can specify exactly what format the string you're parsing is in. MomentJS can then provide you with the underlying Date object, unix timestamp as well as convert to UTC.
var d = moment('2018-03-10 12:12', 'YYYY-MM-DD HH:mm');
console.log(d.toDate());
console.log(d.unix());
console.log(d.utc().toDate());
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.21.0/moment.min.js"></script>
You could of course also parse the date as UTC too instead of treating it as a local time.
moment.utc('2018-03-10 12:12', 'YYYY-MM-DD HH:mm');
NOTE Bit difficult for me to test UTC as I'm in the UK and GMT and UTC are virtually the same.
I have a date string with this format
2016-08-12T15:22:43.698Z
how can I parse it to obtain a resulting string that looks like
Aug 12, 2016 5:22 PM
Is there libraries/component that could facilitate such operation or shall I do it manually by coping each part of the String?
var date = new moment('2016-08-12T15:22:43.698Z');
console.log(date.format('MMM DD, YYYY h:mm A'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.19.1/moment.js"></script>
Use momentjs, and format the moment obj as your requirement.
If the string is in the ISO standard format, which it looks like it is, you can use Date.parse() or new Date() to turn the value into a Date object. With a Date, you can call toString() or toLocaleString() to get the date formatted in local time.
If you are targeting modern JavaScript environments, Intl.DateTimeFormat provides a very complete API for formatting the date in different locales.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DateTimeFormat
I have date string like yyyy-MM-dd HH:mm:ss.SSS which is PST timezone, I need to change it to UTC format 2016-07-28T17:22:51.916Z. How can I do that?
This can be done pretty easily in JS:
var pstDate = new Date(myValidPstDateString);
// ISO is always in UTC time
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toISOString
console.log(pstDate.toISOString());
I am using JSON.Stringfy() to convert my date to String format. but the date so returns is the UTC time for my system.
here is the code.
var dateFrom;
dateFrom=new Date(); // outputs--> Wed Sep 24 2014 16:03:22 GMT+0530 (India Standard Time)
dateFrom=JSON.stringify(x); //outputs--> "2014-09-24T10:33:22.135Z"
//Expected result--> "2014-09-24T16:03:22.135Z"
I think there is something which convert my current date to UTC date. is there any way to get the expected result..any comments would be valuable..
thanks in advance
JSON follows a common format while it converts date objects into strings. Dates are encoded as ISO 8601 strings and then treated as strings while they get serialized.
But it doesnt mean that your date object got modified. It just got represented in a different format which enforced by JSON. So the date still points to the same timestamp which you refer via IST.
So your statement that it alters the date is wrong, it just alters the representation format.
var d = new Date();
//shows local format
alert(d);
djson=JSON.stringify({"date":d});
//shows UTC format
alert(djson);
dobj=JSON.parse(djson);
//again UTC format
alert(dobj.date);
d = new Date(dobj.date);
//again local format
alert(d);