I'm working with a date that looks like this:
Mon Feb 04 2019 15:57:02 GMT-0700 (Mountain Standard Time)
and I'm trying to convert it to this:
2019-02-04T15:57:02.000Z
but for some reason my code always adds 7 hours and ends up being like this:
"2019-02-05T22:57:02.000Z"
Can anyone tell me what I'm doing wrong? Thanks a lot in advance!
Here's my code:
new Date(myTime as string).toISOString();
I'd use Moment.js, which is a decent date parsing and formatting library. To get what you're looking for, you'd use a statement like:
console.log(moment
.parseZone(
"Mon Feb 04 2019 15:57:02 GMT-0700 (Mountain Standard Time)",
"ddd MMM DD YYYY HH:mm:ss 'GMT'ZZ") // the format of the string presented
.local()
.format('YYYY-MM-DDTHH:mm:ss')); // the format of the output
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>
I've broken the single line out into parts so it's a bit easier to read. A few notes:
parseZone allows you to parse the "-0700" from the string.
local converts the date from the parsed time zone to the current time zone
format formats the date.
The format topic has a list of the formatting tokens used.
The Parse > String + Format topic lists the parsing tokens (which are the same as the formatting tokens for the most part).
Note that the output does not have a "Z" at the end; this is important because without the "Z", it is a local date. With the "Z" you are are actually specifying a date and time that is 7 hours earlier than the one you've been given.
I'm not sure how to get this as a one-liner, but this is one way:
var time = new Date('Mon Feb 04 2019 15:57:02 GMT-0700 (Mountain Standard Time)')
new Date(time.setHours(time.getHours() + 7)).toISOString()
"2019-02-05T12:57:02.000Z"
Your code is not adding hours to the input date. What is happening is that your date string is using a particular timezone GMT-0700 (Mountain Standard Time) and the time zone used in new Date().toISOString() is UTC GMT+0000 (UTC). So when in the Mountain Standard Time timezone is Mon Feb 04 2019 15:57:02, in the UTC timezone is actually 2019-02-05T22:57:02.000Z. There are your seven hours from GMT-0700 to GMT+0000.
EDITED
If you don't really care about time zones and want to obtain 2019-02-04T15:57:02.000Z from Mon Feb 04 2019 15:57:02 GMT-0700 (Mountain Standard Time) you could just strip everything after GMT to let new Date() think it is an UTC date.
var timeString = 'Mon Feb 04 2019 15:57:02 GMT-0700 (Mountain Standard Time)';
new Date(timeString.substr(0, timeString.indexOf('GMT') + 3));
2019-02-04T15:57:02.000Z
Related
I'm getting the date in PST TimeZone and need to convert it to local TimeZone. I'm developing a product in CRM so TimeZone can be changed user to user.
I tried to use moment JS but for that, we need to specify the country/ city name and all I'm getting is TimeZoneOffset. I have a code for UTC to local time zone if it gets converted to UTC also then my work will be done.
Fri Jan 17 2020 22:57:49 GMT-0800 (Pacific Standard Time)
Need the answer in TS if possible or JS will be ok. Thank you.
To Convert other time zones to local time zone. You can use the following in javascript.
var date = new Date(other time zone value);
date.toString();
For example
var date = new Date('Fri Jan 17 2020 22:57:49 GMT-0800 (Pacific Standard Time)');
date.toString();
result will be "Sat Jan 18 2020 12:27:49 GMT+0530 (India Standard Time)"
I have an input date with a specific time zone in string. For example:
Sat May 20 2017 17:00:00 GMT-0300 (-03)
I want to change the timezone of this date to my local timezone without converting the time. As if the input date was in my correct timezone.
Sat May 20 2017 17:00:00 GMT+0200 (CEST)
I played around with moment and found this:
const date = moment(moment.utc('Sat May 20 2017 17:00:00 GMT-0300 (-03)').format('LLL'))
Is it the simplest way to do it? I don't like the copy of the moment object but I can't find a method to do it in a "single shot".
To parse your input in local time, you can simply use moment(String, String) function. As docs says:
By default, moment parses and displays in local time.
Here a live sample:
var input = 'Sat May 20 2017 17:00:00 GMT-0300 (-03)';
var m = moment(input, 'ddd MMM DD YYYY HH:mm:ss');
console.log(m.format());
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
I am using moment.js and have date and time in number/timestamp. I want to convert it in my local timestamp i.e, GMT+5:30. Here is jsfiddle link.
code:-
console.log(moment(1489689000000).add(52200000).toDate())
//output: Fri Mar 17 2017 14:30:00 GMT+0530 (IST)
//expected output : Fri Mar 17 2017 20:00:00 GMT+0530 (IST)
Moment
If you want to use plain moment like in your question:
You may try to use moment.utc() if you want your date to be parsed as UTC:
console.log(moment.utc(1489689000000).add(52200000).local().toDate());
or:
console.log(moment(moment.utc(1489689000000).add(52200000).local()));
but this may not actually do what you need - depending on your system config.
Moment Timezone
If you're serious with time zones then you should be using moment-timezone:
var moment = require('moment-timezone');
var zone = moment.tz.guess();
console.log(moment.utc(1489689000000).add(52200000).tz(zone).format());
See:
http://momentjs.com/timezone/
https://www.npmjs.com/package/moment-timezone
Here some other way to achieve this. As per your GMT +5.30 timezone So, you just convert hour to milliseconds.
console.log(moment(1489689000000).add(52200000+ 19800000).toDate());
// here 5.5 hours = 19800000 miliseconds
// add + 19800000 due GMT +5.30
Ideally for your local timezone - moment object will give you the perfect date
moment().toDate() // Fri Mar 17 2017 20:18:53 GMT+0530 (IST)
but if you want for different timezone's, perfect way is to convert using moment timezone
moment().tz(timezone)toDate()
eg. moment().tz('Asia/calcutta').toDate() // Fri Mar 17 2017 20:20:49 GMT+0530 (IST)
I have a database where I have put down timestamps that are converted to UTC.
Now, when I am on PST, I convert 2 timestamps in Chrome, which get back to the following dates:
new Date(1446274800000)
Sat Oct 31 2015 00:00:00 GMT-0700 (PDT)
new Date(1448265600000)
Mon Nov 23 2015 00:00:00 GMT-0800 (PST)
They differ in timezone! How is this possible? I always just want to get back the PST time (or, preferably, the UTC time they were stored in).
Looks like it's daylight savings time.
I need to convert a String to a Date object.
The date string is delivered in the following format:
"2015-01-28T00:00:00"
When I create a new Date, I get the previous date:
Entered: new Date("2015-01-28T00:00:00")
Result: Tue Jan 27 2015 17:00:00 GMT-0700 (Mountain Standard Time)
Does anyone know why this is occurring?
When you enter the following:
new Date("2015-01-28T00:00:00");
// Result: Tue Jan 27 2015 17:00:00 GMT-0700 (Mountain Standard Time)
the browser assumes that you are giving a date in GMT Time zone. So it will automatically convert the given date to your local date.
It's always a good idea to inform the browser of the timezone you are working on in order to prevent future problems:
new Date("2015-01-28T00:00:00-07:00");
// Result: Tue Jan 28 2015 00:00:00 GMT-0700 (Mountain Standard Time)
Actually, you aren't getting the previous date . . . you are getting that date, offset by the timezone difference.
Tue Jan 27 2015 17:00:00(Mountain Time) + 7 hours (time zone difference) = 2015-01-28T00:00:00 (GMT)
Or, in English, when it is 12:00 Midnight in Greenwich, England, it is 5:00 PM on the previous day in Denver, Colorado. ;)
It's the right date/time, just in a different timezone.