Unix epoch time: Adding days - javascript

In Javascript, I needed to add one single day to unix epoch timestamp. So, I tried adding 24*60*60*1000 or 86400000 milliseconds to my date, say: 1396306800000, which, as per http://www.epochconverter.com/ is: 4/1/2014 12:00:00 AM GMT+1. Now, when I try and add one day to it, to get the result, 1396393200000 which is: 4/2/2014 12:00:00 AM GMT+1.
Just one day, right?
Now, I try and subtract a day from the original 1396306800000 to get 1396220400000 or 3/31/2014 12:00:00 AM GMT+1
So far, so good.
Now, If I do one more subtraction, I get this: 1396134000000 which is 3/29/2014 11:00:00 PM GMT+0
How, is this possible? Subsequent subtractions are going smoothly with the same offsets. But at this particular point, 30th March is skipped entirely. Can anyone tell me whats going on?

Daylight savings time. Probably British Summer Time.
You've probably overlooked the GMT+1 in
3/31/2014 12:00:00 AM GMT+1
If you look at the earlier date, it's GMT+0:
3/29/2014 11:00:00 PM GMT+0
The United Kingdom (and other countries) switch time zones on the last sunday of March, which was March 30th this year (2014). The website uses your local time zone settings. This explains the apparent difference of one hour.

It is not skipping a day, you are not accounting for the daylight saving time.
During daylight saving adjustment the day is only 23 hours, which you are subtracting 24hrs from.

I was looking at the link you provided and also calculated and it's giving the correct result.
So if you subtract one day from 1396306800000 so it becomes 1396220400000=1396306800000-86400000
and the output is
GMT: Sun, 30 Mar 2014 23:00:00 GMT

Related

Understanding the Date class

In Javascript/Nodejs I typed x=new Date(2010,0,1) This will mean 1st Jan 2010. But I see 2009-12-31T18:30:00.000Z in the output. What could be the reason for that?
The missing parameters hour, minute and second are initialised to 0, the entire date is assumed to be your timezone (2010-01-01 00:00:00 India time I assume), and the result you see is being output as UTC date. In other words, when it was midnight Jan 1st 2010 in your timezone, it was Dec. 31st 18:30 UTC.
Why it's being output in UTC depends on your method of printing and/or browser used; it's somewhat free to choose how it will output the date.

Moment.js - Adding minute to last minute of hour results in same hour

I'm currently trying to use moment.js (2.24.0) to add a minute to the last minute of a hour. However, it results in going back to the first minute of the same hour.
Here's an example code:
var minute = '2019-11-03 01:59:00';
console.log('Before: '+minute);
minute = moment(minute, 'YYYY-MM-DD HH:mm:ss')
.add(1, 'minutes')
.format('YYYY-MM-DD HH:mm:00');
console.log('After: '+minute);
This results in:
Before: 2019-11-03 01:59:00
After: 2019-11-03 01:00:00
Is this a bug or expected behavior for moment.js? If it's expected behavior, then how do I properly address it to go to the next hour.
In the US, daylight savings time ends at 2AM on 2019-11-03.
The behaviour you're seeing when adding a minute to 1:59AM isn't a bug but rather what will actually happen at that time. The calculation is based on your current timezone so 2019-11-03 01:59:00 EDT (Eastern Daylight Time) + 1 minute = 2019-11-03 01:00:00 EST (Eastern Standard Time).
If you were to run the same test at 2:59 instead of 1:59, it would increment as expected. You could specify that you actually mean 1:59AM EST but be sure that is in fact the case before attempting it.
Additional reading: https://momentjs.com/timezone/docs/#/using-timezones/parsing-ambiguous-inputs/

Crazy time format?

I have an api return an expire time, which looks like a countdown. Im not familiar with milliseconds or whatever this is. The countdown said this number was around a few minutes. I thought it was a unix time, where you could subtract current time from 1970, then convert to minutes but that is giving me a crazy number..
What format is this?
1470041268756
There's nothing crazy about it. Well, nothing beyond normal UNIX craziness :-)
If you head on over to the Unix epoch converter and plug that value in, you'll see:
Assuming that this timestamp is in milliseconds:
GMT: Mon, 01 Aug 2016 08:47:48.756 GMT
which is certainly viable in terms of being a few minutes after you formed the question.
As you haven't given much information, I would have to guess that it is unix timestamp but in milliseconds. So if you divide by 1000 first (essentially remove the last 3 digits) and then convert you would get the following:
Mon, 01 Aug 2016 08:47:48 GMT
I would expect you can find a conversion function that works with milliseconds too though. In fact, with javascript the Date object will take this format (in milliseconds):
var date = new Date(1470041268756);
console.log(date); // Mon Aug 01 2016 09:47:48 GMT+0100 (GMT Summer Time)
They look like milliseconds since 01/01/1970 00:00
see here
you may use moment.js
alert(moment.unix(1470041268756/1000).format('MMMM Do YYYY, h:mm:ss a'));
<script src="http://momentjs.com/downloads/moment.js"></script>

getUTCDate() Function Issue in Different Time Zones

I am facing very strange issue kindly take a look at two examples below.
My Development Environment Time Zone: GMT+0500
When i use following function:
var d = new Date("Tue Mar 18 2014 00:00:00 GMT+0500");
var n = d.getUTCDate();
n = 17 which is correct (Return the UTC day of the month of a specific, local time, date-time) and everything works perfectly in my timezone.
My Clients Time Zone: GMT+0000
var d = new Date("Tue Mar 18 2014 00:00:00 GMT+0000");
var n = d.getUTCDate();
n = 18 which is wrong
any one put some light why is that? how to resolve this issue?
any help would be appreciated.
n = 18 which is wrong
No it's not. You supplied GMT+0000, which is the same as GMT or UTC. So the result from getUTCDate is of course the date you passed in.
I think you are confused because of how you worded this:
My Development Environment Time Zone: GMT+0500
My Clients Time Zone: GMT+0000
A time zone is not a numeric offset. A time zone can have an offset, or multiple offset, and includes the history of how the offsets have changed over time. See "Time Zone != Offset" in the timezone tag wiki.
So those might be the current offsets for you and your client, but that doesn't necessarily mean that they are always going to be in the same offset. If your client is in the UK, then they are at +0000 now, but they will soon be on +0100. See here for details.
Your first date is explicitly constructed with a time zone that results in the UTC date being 17. At midnight of the 18th in the timezone 5 hours ahead of UTC (GMT), it's still the 17th in London. Your second date is constructed with the explicit UTC timezone. At the time indicated by your second date, in other words, it's 5 o'clock in the morning in the first timezone.

Setting Javascript date object to midnight without being based on user computer date

I need to set a date and time to 12 midnight regardless of the user's computer date. I am creating an international meeting planner to offset time zones to get the meeting.
I have it working but I need to code now the differences in time zones. If I use new Date, it gives me the time based on the user's computer. For example, mine is Eastern US. If I try to do the time zone shift on November 6, 2011, Javascript/computer will calculate MY time zone shift at 2am. I do not want this.
My real goal is to set it to 12 midnight in the timezone that is where the meeting will be hosted from (let's say Afghanistan) and then calculate from there.
So:
How do I set 12 midnight without being being the user's computer time?
Can I set 12 midnight to a specific time zone, without being dependent on user's computer time?
I have to do this with Javascript as there is no server code involved.
Thanks
Something like this I would guess: new Date(Date.UTC(year, month, day, hour, minute, second))
w3schools Reference
To create midnight in GMT:
// Note: months are 0-based, so 7 == august
var midnight = new Date( Date.UTC(2011,7,15) );
// Sun Aug 14 2011 18:00:00 GMT-0600 (Mountain Daylight Time)
To create midnight in another time zone:
var mdt = -6; // Mountain Daylight Time
var midnightMDT = new Date( Date.UTC(2011,7,15,-mdt) );
//-> Mon Aug 15 2011 00:00:00 GMT-0600 (Mountain Daylight Time)
Date objects are expressed in the local time zone of the user, but they are still representative of midnight in another time zone.
If you want to express a date in another time zone, you'll need to offset the date to that timezone (setUTCHours()) and then use the various getUTC* methods (e.g. getUTCHours()) to construct your own string.

Categories