Moment js time stamp - javascript

I am using moment.js to parse my date time and I am getting totally confused.
If I do moment().toDate() I get:
Fri Aug 21 2020 21:52:48 GMT-0400 (Eastern Daylight Time)
which is exactly correct. I am showing this so that you know that moment is not pulling incorrect local time for me.
I have a time which looks like this that I pulling from a database:
2020-08-21T21:49:58.000Z
As you can see this is the same day as above but a slightly different time like 3 minutes ago.
If I do:
moment("2020-08-21T21:49:58.000Z").calendar()
it returns :
"Today at 5:49 PM"
which is incorrect.
However, if remove 0Z from the end of the time stamp and do this:
moment("2020-08-21T21:49:58.00").calendar()
I get
"Today at 9:49 PM"
which is correct.
I am guessing by looking at the format moment decides it is a UTC time and subtract 4 hours from it.
Therefore, I thought I would convert it back to utc() but when I do:
moment("2020-08-21T21:49:58.000Z").utc().calendar()
I get:
"Yesterday at 9:49 PM"
which decreases it by 24 hours and I am not sure why.
Is there a function that I can use to prevent it from assuming that the time is in UTC format?
Edit 1:
If I do moment().toDate() I get:
Sun Aug 23 2020 20:53:28 GMT-0400 (Eastern Daylight Time)
which is correct.
When I try to use the time stamp from today:
moment("2020-08-23T20:52:38.000Z").utc().calendar()
I am getting:
"Yesterday at 8:52 PM"

You could try to enable your moment object to local time mode:
moment("2020-08-21T21:49:58.000Z").local().calendar()

Related

Start of Day from one timezone to GMT

I am using moment-timezone library to build a UI that needs to be relative to a variety of timezones.
I am taking an input of a timezone, i.e "America/Chicago" and need to get the start of day in GMT.
For instance, if today is March 27th at 9am Chicago time (2pm GMT), I need to get the date in epoch seconds for March 27th, 00:00 AM.
I'm using the moment.tz("America/Chicago").startOf('day') but I keep getting Tue Mar 27 2018 01:00:00 GMT-0400 (EDT) . Any idea how to do this?
Thanks
// This part you are already doing correctly.
// You get back a Moment object representing the start of the current day in Chicago:
var m = moment.tz("America/Chicago").startOf('day');
I need to get the date in epoch seconds
// Ok, so simply now get the associated Unix Time
var timestamp = m.unix();
Also note that the correct terminology is "Unix Time", not "epoch seconds".
See my blog post: "Please don't call it Epoch Time".
... but I keep getting Tue Mar 27 2018 01:00:00 GMT-0400 (EDT)
You are probably either looking at _d or a Date object, or rather the string representation of one. Don't. See this Moment.js documentation for details.
Per your comment:
I need to take the current time in a specific timezone. I then need to convert that time to the corresponding day in GMT. Finally I need to get the midnight epoch timestamp of that GMT day.
That's a little different then you originally asked, but it would be like this:
var timestamp = moment.utc().startOf('day').unix();
Note that there's no purpose in involving another time zone for this operation. Logically, when asking for "Now in time zone A converted to time zone B", it's the same as asking for "Now in time zone B". In other words, you would get the same value even when the time zone was present:
var timestamp = moment.tz('America/Chicago').utc().startOf('day').unix();
So you're better off just leaving the time zone out.

Getting the value from momentJS subtract without a time zone or offset (GMT +0000)

I wish to produce a date 30 days in the past using momentJS - it's pretty easy, I just use the following
const date30DaysPast = moment().utc().subtract(30, 'days').toDate(); // Sun Oct 29 2017 13:23:46 GMT+0100 (CET)
This is all great however I want the returned date to have no Time Zone or offset, I want the time to be GMT+0000 not as above GMT+0100 (CET) - for example:
Sun Oct 29 2017 13:23:46 GMT+0000
I wish to force this as I am using testing servers that are in different locations, and rather than take the time from the local browser I just want set a standard time. I thought using the utc method would do this for example should I write something like this in my test:
const oct4th2017 = moment.utc(new Date('October 04, 2017 11:13:00'));
the output is
Wed Oct 04 2017 09:13:00 GMT+0000
How can I remove the offset / time zone and set it to GMT on my original subtract method? I have tried wrapping in a parent utc method like so
const date30DaysPast = moment.utc(moment().utc().subtract(30, 'days').toDate());
but this doesn't work. I get the momentJS object.
Any advice could be appreciated, should my wording be bad or confusing please say so and I shall reword my question.
You already have your moment.js object without timezone. But the toDate() method creates a native Date object, which always has the local timezone (though it has various UTC methods).

When I try to create a date object from another date format, the result date is changing it's value

When I try to create a date object from another date format, the result date is changing it's value. How to achieve this without changing the date value ?
new Date("Mon, 31 Oct 2016 00:00:00 GMT");
the result coming as Sun Oct 30 2016 20:00:00 GMT-0400 (Eastern Daylight Time), How can I get the Monday 31 date from the above?
Adjusting the timezoneOffset from the created date object should do the trick,
but be cautious while using it , as you should be sure that the date object was created from GMT not from some local time .
And the below answer has been posted assuming the input date was in GMT
var tempDate = new Date("Mon, 31 Oct 2016 00:00:00 GMT");
var tempTime = tempDate.getTime() + (tempDate.getTimezoneOffset() * 60000);
tempDate = new Date(tempTime);
console.log(tempDate);
It doesn't change the date, it just converts it to your local timezone. This is a bit of annoying behaviour and the only way I know to get around it is to set your system timezone to GMT. If you need to do date and time work, you might want to look at Moment.js - http://momentjs.com/

Javascript date formatting - one hour out due to daylight saving

So now, its 9:23am. I have a UTC date string that represents the current date, that looks like this "2012-07-17T09:23:27.75"
I want that in a date object, so I can display a nicely formatted date, so I:
var myDate = new Date("2012-07-17T09:23:27.75")
// Gives --> Tue Jul 17 2012 10:23:27 GMT+0100 (GMT Daylight Time)
So because of daylight saving time I'm getting an hour-out issue. I can see that myDate.getTimezoneOffset() gives me -60, what's the standard / best practice way to get my date to actually reflect the current correct time? Have I just entered javascript date hell?
Try momentjs.com. I really found it handy for such things.
var myDate = moment("2012-07-17T09:23:27.75");
Gives you a date instance in your timezone (that basically configured on your computer). Moreover momentjs has nice human friendly formattings like "a couple of seconds ago", "a month ago",...
Dates are really a hell in JS (but not only in JS). The best thing you can do is to always only transport in UTC between browser <-> server. Then on the server convert it to what time format you like, you obviously only have to be consistent. That way I managed to handle date-times properly.
Try removing the 'T'
I was debugging some date time format issue in chrome when I found out that in console
new Date('2016-04-16T15:15:00') returns Sat Apr 16 2016 16:15:00 GMT+0100 (GMT Daylight Time)
while
new Date('2016-04-16 15:15:00') returns Sat Apr 16 2016 15:15:00 GMT+0100 (GMT Daylight Time)

Make a date object take timezone into consideration

I'm using the following piece of code:
$.log('Ending time: ' + ending_time);
$.log('Now: ' + new Date());
$.log('Difference: ' + new Date(ending_time - new Date()));
The output is the following:
Ending time: Thu Apr 23 2009 14:31:29 GMT+0200
Now: Thu Apr 23 2009 11:56:02 GMT+0200
Difference: Thu Jan 01 1970 03:35:26 GMT+0100
I'm using the "difference" to display how many hours and minutes there are left until ending_time, but because of the timezone differences, I get the wrong time (offset by one hour.) So is there any neat way of calculating the difference taking timezones into account?
You are no longer dealing with a date, so don't convert it to one. You have a time difference, which doesn't have a time zone for instance. The result should be in milliseconds, so perform the appropriate math to get minutes, hours, days, or possibly all of the above as needed.
You should be able to use the getTimezoneOffset function.
Check it out here.
You can use the following:
(new Date()).getTimezoneOffset()
which will give you the timezone offset of the client's browser in minutes. Of course you also need to know the timezone offset of ending time.

Categories

Resources