We currently save the date in this format: 12/12/2011 8:00:00 PM.
When I parse it in JS Date object, it converts into this: Tue, 13 Dec 2011 00:00:00 GMT.
I do not believe this is a timezone issue because we are -4 GMT and it does not add up. Any ideas?
var start = new Date("12/12/2011 8:00:00 PM");
alert("12/12/2011 8:00:00 PM");
alert(start.toUTCString());
It seems to add up perfectly. "12/12/2011 8:00:00 PM" in GMT-4 is exactly "Tue, 13 Dec 2011 00:00:00 GMT".
EDIT
Use toLocaleString to get the date and time in your local time zone.
start.toLocaleString()
Related
I am using Moment Timezone to get current datetime for Asia/Tokyo timezone.
The code is as following
var currentDateTime = new Date(moment().tz('Asia/Tokyo').format('YYYY-MM-DDTHH:mm:ss'))
Supposed that the current datetime for my local timezone is as following
Thu Jul 22 2021 19:49:47 GMT+0700 (Indochina Time)
I expected the current date for Asia/Tokyo timezone would be as following
Thu Jul 22 2021 21:49:47 GMT+0700 (Indochina Time)
In Chrome I got the expected datetime.
But in Safari on my iPhone, I got the wrong datetime.
Fri Jul 23 2021 04:49:47 GMT+0700 (Indochina Time)
It seems the current datetime returned is the right current datetime plus 7 hours.
Here is my environment
iPhone : iPhone 6
iOS : 12.5.4
The problem you have is caused by the Date constructor parsing the time according to the local timezone (in Chrome) and UTC (on Safari?).
For the time given, this code
moment().tz('Asia/Tokyo').format('YYYY-MM-DDTHH:mm:ss')
returns
"2021-07-22T21:49:47"
You then pass this time into the Date constructor:
var currentDateTime = new Date("2021-07-22T21:49:47")
Because this string has no time zone information, it is assumed to be in the device's local timezone (Chrome) or UTC (Safari) which gives the wrong date object.
While you could inject the timezone into this string (using ZZ), you can build a Date object from the moment object using:
var currentDateTime = moment().tz('Asia/Tokyo').toDate()
but this is effectively the same as
var currentDateTime = new Date()
If the intended output is a string of the format:
"Thu Jul 22 2021 21:49:47 GMT+0900 (Japan Standard Time)"
the closest you could get is:
moment().tz('Asia/Tokyo').format('ddd MMM D YYYY, h:mm:ss a [GMT]ZZ (z)')
// Thu Jul 22 2021 21:49:47 GMT+0900 (JST)
Alternatively, there is also the localized "Month name, day of month, day of week, year, time" format:
moment().tz('Asia/Tokyo').format('llll (z)')
// In my locale, I get:
// Thu, Jul 22, 2021 9:49 PM (JST)
Take a look at the Moment display format documentation for more options.
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)
when i create a new Date Object from a string i get a wrong date by one day and I dont understand why. I've already verified, that it does not come from timezone difference.
var myDate new Date("2016-04-12T22:04:00.000Z")
console.log(myDate);
Wed Apr 13 2016 00:04:00 GMT+0200 (Central European Summertime)
The issue is timezones. 22:04:00 in UTC is 00:04:00 in UTC+2.
To get the UTC time, fetch the date parts with myDate.getUTCHours(), mydate.getUTCMinutes(), etc. See a full list of Date methods.
You can get the UTC string of your date-
var myDate=new Date("2016-04-12T22:04:00.000Z");
myDate.toUTCString()
/* returne value:
Tue, 12 Apr 2016 22: 04: 00 GMT
*/
I'm trying to subtract 1 hour from the current date, I'm using this code:
var date = new Date();
// date is **Fri Apr 03 2015 16:47:33 GMT+0100 (GMT Standard Time)**
var uploadDateFilter = new Date(new Date(date).setHours(date.getHours()-1)).toISOString();
//now date is **Fri Apr 03 2015 14:47:33 GMT+0100 (GMT Standard Time)**
There's less two hours instead of just one. What am I missing here?
Your code works when looking at the elements in the same format.
<div class="original"></div>
<div class="updated"></div>
Using jQuery to post the information ... with your code, as is.
$(".original").text(date.toISOString());
$(".updated").text(uploadDateFilter);
Results in ...
2015-04-03T16:00:23.441Z
2015-04-03T15:00:23.441Z
http://jsfiddle.net/rfornal/5nma5uhr/
You don't need .toISOString(), but for me it works :
var date = new Date();
var uploadDateFilter = new Date(new Date(date).setHours(date.getHours()-1));
console.log(date);
console.log(uploadDateFilter);
Console output :
Fri, 03 Apr 2015 15:59:48 GMT
Fri, 03 Apr 2015 14:59:48 GMT
http://repl.it/gxL
The reason appears to be daylight saving check this my fiddle.
For me in the UK the clocks went forward on Mar 29, 2015. Checking the effect of new Date().toISOString() before and after this date, during daylight saving the result is one hour less.
I'm creating dates from strings with the format 'yyyy-MM-dd' but they're always created on the previous day for some reason. If I set the date as '2012-10-31' the Date object with actually be 30 of October and not 31. For example, this:
var d1=new Date('2012-10-31');
Will output this:
Tue Oct 30 2012 19:30:00 GMT-0430 (Venezuela Standard Time)
Can someone explain why this happens?
With no further parameters, Date() create your time-stamp with GMT+0000.
Converting your date to string with no further parameters either, it will use the localised notation.
If you want to create a date matching your time-zone, do:
var d1=new Date('2012-10-31 GMT-0430');
//That's what you should get
//"Wed Oct 31 2012 00:00:00 GMT-0430"
Using this date now, you can convert the local time to the time of other timezones if you execute d1.toString() in a browser with a different timezone:
d1.toString();
//That's what I get
//"Wed Oct 31 2012 05:30:00 GMT+0100"
Try this
var d1=new Date(2012, 10-1, 31, 0, 0 ,0);
document.write(d1);
which produces
Wed Oct 31 2012 00:00:00 GMT-0400 (Eastern Daylight Time)
the key is to remove the quotes and manually set the time. Also note that 'month' is zero based and so for readability I subtract one from it
This happens because dates are converted to a string based on your local time zone.
The date variable actually contains October 31st 0:00 UTC. When you convert it to string, it is converted using your own time-zone, which is 4:30 hours behind UTC.