How to add timestamp in local time in momentjs - javascript

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)

Related

Moment date conversion from datepicker format problem

I have a problem with converting date time which comes from datepicker to moment (I use this library) time format.
What I get from datepicker: 2021-01-30T07:00:00.000Z
The code that I used via moment:
let tempTime = moment(dateString).toDate()
Output I get with this implementation: Fri Apr 30 2021 00:00:00 GMT-0700 (Mountain Standard Time)
What I expected: Fri Apr 30 2021 07:00:00 GMT-0700 (Mountain Standard Time)
The difference is between hours. In my implementation they just being ignored.
How can I overcome this issue?
Thanks for your attention!
The problem with conversion is the date string is in an UTC format. Either you remove the Z at the end or you can provide custom format and escape the UTC identifer like below.
const dateString = "2021-01-30T07:00:00.000Z";
let tempTime = moment(dateString, "YYYY-MM-DDTHH:mm:ss[Z]").toDate();
console.log(tempTime);

How to convert PST timezone date to Local TimeZone in Javascript?

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)"

How to convert time to specific string?

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

Transpose date into local timezone without shifting the date

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>

Converting Timestamp to Date gives different timezones in the result?

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.

Categories