Convert UTC date as it is to target format - javascript

I have date in 2019-12-07T14:55:00.000Z which I want to convert to target format without effecting original value by browser's timezone using momentjs. I tried moment('2019-12-07T14:55:00.000Z').format('MM/DD/YYYY HH:mm'), but it is converting it to 12/07/2019 6:55 (because browser timezone is PST) whereas I expect it to be 12/07/2019 14:55

we can use utc function to prevent moment to use browser's timezone , just like below
moment.utc('2019-12-07T14:55:00.000Z').format('MM/DD/YYYY HH:mm')

Related

Convert date with timezone to UTC?

A quick question. I have a ISO string date:
2022-07-03T10:51:09+02:00
this date as you can see has timezone included (+02:00).
Question: How to convert it into UTC date? Using e.g. date-fns or moment?
Edit: Should I just simply add "02:00" hours to current date? So it would be 12:51:09?
Trivially new Date(isoString).toISOString(), no libraries required.
const input = "2022-07-03T10:51:09+02:00";
console.log(`${input} in UTC:\n${new Date(input).toISOString()}`);
In my view, timezone is simply the representation of the same timestamp across different geographies (no. of seconds elapsed since unix time 0 is the same everywhere). So be careful while adding/removing time manually from the existing timestamp.
You can do that using moment.js like this:
var someday = moment('2022-07-03T10:51:09+02:00');
console.log(someday.utc().format());
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.4/moment-with-locales.min.js"></script>

Converting a DateTime Object to a specific timezone

I have a dateTime string "2019-02-14 17:18:22".
I would like to convert this above-mentioned dateTime string to a specific timezone dateTime .
Here the timezone will be extracted from another dateTime string - "2019-02-14T17:28:24+08:00".
I did look up at the utcOffset function but I don't know how to use the offset value (330 in mycase).
Expected result: The first String is fairly simple 5:18 PM .
But once getting converted to the specific timezone, it will be 2:48 PM.
Heres how I use timezone offsets.
I get DateTimeIn, which is offset to (UTC+00:00) from the server.
Then to convert to the browser's timezone (UTC-05:00), i use: getTimezoneOffset() to update the object to the local timezone.
var dateObj = new Date(DateTimeIn);
dateObj.setMinutes(dateObj.getMinutes() + dateObj.getTimezoneOffset());
Without this, my server downloaded datetimes are offset and unusable.

moment date parsing in JS

Using moment js
var y = moment('2016-01-11T16:00:00');
console.log('UTC ' + moment().utc().format());
console.log('Local ' + moment().format());
console.log(y.format());
console.log(y.toISOString());
Above code outputs
UTC 2016-07-12T19:54:15Z
Local 2016-07-12T15:54:15-04:00
2016-01-11T16:00:00-05:00
2016-01-11T21:00:00.000Z
Why it is displaying -05:00 offset in y.format().
.format() is designed to take a parameter that allows you to format the date exactly how you want to display it. Since you don't pass a parameter, it gives you the default, which in this case, shows the timezone (-05:00). Per the docs,
As of version 1.5.0, calling moment#format without a format will
default to moment.defaultFormat. Out of the box, moment.defaultFormat
is the ISO8601 format YYYY-MM-DDTHH:mm:ssZ.
As of version 2.13.0, when in UTC mode, the default format will return
Z as the offset, instead of +00:00.
Check the link for info about formatting a date with .format().
moment will default to ISO 8601 (Local time with offset to UTC), have a look a the documentation at moment docs, and more on the standard itself at ISO 8601 Standard

MomentJS set timezone

Because of how we store dates I need to set a moment object to timezone +0000.
I've tried using a variety of ways:
var d = moment().hour(0).minute(0).second(0).millisecond(0).zone('+0000');
var d = moment().hour(0).minute(0).second(0).millisecond(0).utc(0);
var d = moment().hour(0).minute(0).second(0).millisecond(0).utc();
When I console.log these dates they come out with the time 00:00:00 GMT+0100 (BST)
Looking at the documentation it seems to say that .utc() and .zone() are for printing a format only, is this true? (This is the same I've seen with other questions on here, none address setting the actual object to a timezone it seems)
After I set and then manipulate the date I convert it to the JS Date object to use with angular-ui bootstrap datepicker (note: it was a moment object which I used console.log on).
Unless you specify a timezone offset, parsing a string will create a
date in the current timezone.
http://momentjs.com/docs/#/parsing/string-format/
Example: You can tell in which timezone is your date using
moment ('2015-05-06T23:00:00.000Z').
If you need to convert this to a specific timezone you can do so:
moment().utc(0).format('YYYY-MM-DD HH:mm Z').
About Timezone in Javascript: How do I specify the time zone when creating a JavaScript Date?

Moment.js round dates up

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');

Categories

Resources