Going out of mind with this. So I have the following:
var now = moment().local();
var timestamp = now.unix();
But the timestamp is always one hour behind and I cant figure out why. If I log now in the console I get what I expect:
Tue Jun 09 2020 19:19:32 GMT+0100 (British Summer Time)
But when I log timestamp to the console I get
1591726782
Which equates to:
Tue, 09 Jun 2020 18:19:32 GMT
Driving me completely potty. Probably a really simple solution but I have trawled the web and checked MomentJS documentation and cant see where I am going wrong. Any help would be greatly appreciated.
Related
I am trying to display a date in javascript. I receive the date from backend like this: 2020-09-22T17:10:25Z (from and Instant object in Java).
When I try to call new Date("2020-09-22T17:10:25Z") I get: Tue Sep 22 2020 20:10:25 GMT+0300 (Eastern European Summer Time). The issue with this is that I am not in a GMT+0300 timezone but rather GMT+0200.
When I try to call new Date() on the other hand I get Thu Dec 08 2022 20:34:11 GMT+0200 (Eastern European Standard Time) which is my correct timezone.
My question is, why in the first case I get the GMT+0300 and in the second case I get GMT+0200? The Z in the string I am trying to parse stands for Zulu or zero hour offset, so why does the 2 different approaches use different timezones?
It looks like you are in GMT+2 in winter, but in summer (in September) you are in summer time which is GMT+3
javascript's date() function works off of the time set on your local computer. if it is giving GMT+3, then your computer is set to GMT+3. check your system clock's configuration.
windows: https://kb.wisc.edu/helpdesk/page.php?id=79027
mac: https://support.apple.com/en-ca/guide/mac-help/mchlp2996/mac
linux: https://www.makeuseof.com/how-to-set-date-and-time-linux/
I'm trying to convert a timestamp to a moment js object, like this:
let obj = moment.unix(1459382400);
It returns Wed Mar 30 2016 20:00:00 GMT-0400 (CDT) which is wrong, because the given timestamp corresponds to Thu, 31 Mar 2016 00:00:00 GMT.
Am I missing something? What am I doing wrong?
It is because of your timezone, do .utc() at the end.
Out of the box, momentjs is using your local timezone, so I guess there might be your problem.
You can use let obj = moment.unix(1459382400).utc(); instead, which should give you what you need.
Example here: http://jsfiddle.net/rLjQx/2544/
corresponding docs here: https://momentjs.com/docs/#/parsing/utc/
I am using the ionic framework and I am trying to calculate the number of days between today and Sun Apr 18 2017 15:14:36 GMT+0800 (MYT)
and today :Thu Mar 09 2017 17:52:48 GMT+0800 (MYT)
it's showing that there are 44 days but I know it's 40 days. Can someone help me fix my code
$scope.daysLeft=Math.floor(Math.abs(newDate - new Date())/86400000);
You are wrong and the result is actually ok from your program.
There are 44 days between today and the selected date.
Just try here if you don't believe it.
Can someone explain to me why if I paste the following into the consoles of both Chrome and Firefox I get different results
new Date("2014-12-01")
Output in Chrome:
Sun Nov 30 2014 18:00:00 GMT-0600 (CST)
Output in Firefox:
Date 2014-12-01T00:00:00.000Z
Yes, you're reading that correctly. Chrome renders it as a day before.
Each browser will show dates in the console like it wants.
In your case, Firefox seems to use Date.prototype.toISOString under the hood:
new Date("2014-12-01").toISOString(); // "2014-12-01T00:00:00.000Z"
And Chrome seems to use Date.prototype.toString. In my case, on Chrome and in my timezone, I get
new Date("2014-12-01");
// Mon Dec 01 2014 01:00:00 GMT+0100 (Hora estándar romance)
new Date("2014-12-01").toString();
//"Mon Dec 01 2014 01:00:00 GMT+0100 (Hora estándar romance)"
However, note that Chrome does not render it as a day before. It's just that it uses GMT-0600:
Date.parse("Sun Nov 30 2014 18:00:00 GMT-0600 (CST)"); // 1417392000000
Date.parse("2014-12-01T00:00:00.000Z"); // 1417392000000
I think the Chrome's reading has to do with your locale settings as it converts the GMT entry to your time zone and makes the necessary adjustments whether subtracting, adding, or leaving it as is.
I think I need clarification on something:
I have a string representing a date in a format like this:
'2013-12-24 12:30:00'
and if I pass it to Date(), then I get the following output
new Date('2013-12-24 12:30:00')
// --> Tue Dec 24 2013 12:30:00 GMT+0100
because iOS has problems with this, I read that I should use T as separator, however
new Date('2013-12-24T12:30:00')
// --> Tue Dec 24 2013 13:30:00 GMT+0100
the result adds one hour. I guess it has something to do with summer or winter, but what exactly does the T stand for, and why is the result different? I meanwhile solved my problem by passing separate parameters to the Date but I would still like to know where this extra hour is coming from.
new Date('2013-12-24T12:30:00')
treats the time as UTC, so it's 12:30 in Greenwich and 13:30 in your timezone.
new Date('2013-12-24 12:30:00')
is a Chrome extension (or bug) that doesn't work in other browsers. It treats the time as local, so it's 12:30 in your timezone (GMT+1) and 11:30 in Greenwich.
If you look closely... you will notice adding a T makes the time in 24-hour format.
new Date('2013-12-24T12:30:00')
// --> Tue Dec 24 2013 13:30:00 GMT+0100
Compared to
new Date('2013-12-24 12:30:00')
// --> Tue Dec 24 2013 12:30:00 GMT+0100
I guess its stands for Long time pattern. Refer this for more.