I am receiving times in the an AJAX request and am converting them using the new Date() function.
I receive 2013-06-18T12:00:15Z
However, somehow I get the following after new Date():
Tue Jun 18 2013 08:00:15 GMT-0400 (EDT)
Why is it not:
Tue Jun 18 2013 12:00
See the following demo:
http://www.w3schools.com/js/tryit.asp?filename=tryjs_date_convert
This is a time zone problem. You must be in the EDT timezone (GMT-0400). To correctly parse the date you should tell the parser in which timezone your date is correct.
For you parse your date like this :
new Date('2013-06-18 12:00:15 GMT-0400')
"GMT-0400" means GMT time minus 4 hours
Or if you don't wish to reformat your string, you can use the date.getUTC* functions to get the time as you parsed it.
The full list is available at Mozilla's documentation.
I agree with Vaim Caen's answer that this is a timezone issue, but not with parsing - the date is being parsed fine, but into your local timezone, while you're expecting it to be parsed into UTC date.
This answer shows how to convert from your current timezone to UTC - applying this to the TryIt demo gives:
var msec = Date.parse("2013-06-18T12:00:15Z");
// or: var msec = Date.parse("Tue Jun 18 2013 08:00:15 GMT-0400 (EDT)");
var d = new Date(msec);
d.setTime( d.getTime() + d.getTimezoneOffset()*60*1000 );
document.getElementById("demo").innerHTML = d;
Edit: If you all you're interested in is displaying the date (no further manipulations) then you can use:
d.toUTCString()
which will show the date in GMT (for me it actually shows "GMT" so most likely not of use!)
The alternative is to add a function to the prototype to show the date in whatever format you want and use the date.getUTC* methods.
Related
I get from data base this date string:
var MeasureDateStr = "2016-07-19T16:29:31";
On client I create datetime javascript object from MeasureDate value:
var measureDate = new Date(MeasureDateStr);
After the measureDate object is created the content is:
var measureDate = Tue Jul 19 2016 19:29:31 GMT+0300 (Jerusalem Daylight Time);
As you can see I have different time (+3 hours) relatively to original date string.
My question is why I get diffrent time in measureDate and how to fix the problem?
You get user local time because you don't have anything to tell browser in which timezone to look at.
If you want to have UTC time, just add Z at the end of time string:
var MeasureDateStr = "2016-07-19T16:29:31Z";
Also this may help:
How to ISO 8601 format a Date with Timezone Offset in JavaScript?
Are you sure?
The string you are reporting is a local time string.
If I run your example, I get a Tue Jul 19 2016 16:29:31 GMT+0200 (ora solare Europa occidentale), so the local time is preserved, while a time zone is attached.
If you add a Z, or a time zone indication (like 2016-07-19T16:29:31+03:00), you can specify exactly the behaviour you want.
Please note that when you log the date, you will always get a local format, even if the date was specified with another time zone:
var MeasureDateStr = "2016-07-19T16:29:31+03:00";
var measureDate = new Date(MeasureDateStr);
console.log(measureDate);
I get Tue Jul 19 2016 15:29:31 GMT+0200 (ora solare Europa occidentale) because I'm on GMT+2. But the time is converted accordingly.
It's because your browsers timezone is set to GMT+3. And since you do not specify a timezone in your datestring it will add 3 hours automatically.
EDIT:
var d = '2016-07-19T16:29:31';
var offset = new Date().getTimezoneOffset() * 60 * 1000; // get Timezone offset in milliseconds.
d = new Date(Date.parse(d) + offset) // Remove the timezone offset.
console.log(typeof d); // object
console.log(d); // Tue Jul 19 2016 16:29:31 GMT+0200 (CEST)
Old answer:
new Date('2016-07-19T16:29:31').toUTCString()
I have a problem here. I need to change the timezone of of a date in Javascript to UTC before passing it to the back end pf my service for validation. I cannot find a solution in any of the questions on this site or on other sites. The problem is that every method I have tried so far is also changing the time and date of my Javascript date object. For example:
var startDate = new Date($("#start-date-picker").val()).toUTCString();
Changes the time which affects the date (the timezone of my laptop is currently set to GMT +2).
While debugging my date object looks like this:
var startDate = new Date(getProperDate($("#start-date-picker").val()));
//startDate = Wed Aug 26 2015 00:00:00 GMT+0200 (Romance Daylight Time) {}
But when changed using the .toUTCString() method the date ends up like this:
startDate2 = "Tue, 25 Aug 2015 22:00:00 GMT"
I cannot find a way to change just the timezone and preserve the current date and time. I cannot use any external libraries either before anyone suggests moment.js or any others. Any help would be much appreciated, thanks!
I'm sending the server time object with zero date, the sent date is:
Thu Jan 01 1970 01:02:01 GMT+0200
How can I convert it to GMT+0000? I need to tell the server about some task duration, so I want it to be just 01:02:01 as a duration. But the sent date is local and the server understands it as 03:02:01! How can I zero the GMT index?
Thanks
Getting the GMT time out of a JavaScript Date object is simple enough -
Date.prototype.toUTCString()
The toUTCString() method converts a date to a string, using the UTC time zone.
For example:
var test = new Date('Thu Jan 01 1970 01:02:01 GMT+0200').toUTCString();
console.log(test);
Note that this correctly outputs Wed, 31 Dec 1969 23:02:01 GMT, which although it not what you are looking for, is converting the provided Date to GMT.
To get what you want out of your input, a regular expression is useful. Caveats:
assumes duration will never be more than 23 hours, 59 minutes, and 59 seconds. If it is this will break.
var test = 'Thu Jan 01 1970 01:02:01 GMT+0200';
var durationMatcher = /\d\d:\d\d:\d\d/;
console.log(test.match(durationMatcher));
If you can, consider working in some values that works for you with one number - number of milliseconds for example.
function convertToGmt(pdate)
{
var newDate = new Date(pdate);
return (newDate.getUTCHours()<10?"0"+newDate.getUTCHours():newDate.getUTCHours())+":"+(newDate.getUTCMinutes()<10?"0"+newDate.getUTCMinutes():newDate.getUTCMinutes())+":"+(newDate.getUTCSeconds()<10?"0"+newDate.getUTCSeconds():newDate.getUTCSeconds());
}
Now use this function and call it by passing you date.
Notice that getUTCHours() returns correct hour in UTC.
Working Fiddle
I have a timestamp given by
timestamp = 2015-02-22T10:00:00.000Z
Why is it converted to GMT when I do this
var dt = new Date(timestamp);
console.log('dt = ' + dt); // prints Sun Feb 22 2015 05:00:00 GMT-0500 (EST)
I don't want it to convert my date to GMT. How do I prevent javascript from converting my dates?
When you try to execute dt = ' + dt, Javascript tries to convert the dt object to a string so it can be added to another string. It does that by calling the dt.toString() method and the format you are seeing is the default string conversion for a date object.
FYI, this default format that looks like this:
Fri Mar 06 2015 19:24:42 GMT-0800 (Pacific Standard Time)
is NOT GMT time. The time value shown is local time. It is showing you that local time shown is -0800 hours from GMT, but the time itself is expressed in local time.
It's not uncommon to want to just truncate off the last part of this and display:
Fri Mar 06 2015 19:24:42
That can be done like this:
console.log('dt = ' + dt.toString().replace(/\sGMT.*$/, ""));
Working demo: http://jsfiddle.net/jfriend00/hg5m0r1r/
If you want something different to show, then you should construct the string representation you want yourself rather than letting the system automatically call .toString(). You can look at the Date object methods available and decide what you want to display. A Date object internally is a number of ms since the epoch time so any string representation is a conversion of some kind. You have to tell it what conversion you want.
You can see a list of the many date methods here.
I'm simply trying to take an input string and convert it to a date object.
moment.utc('2000-01-01T00:00:00.000Z').toDate()
But it returns this...
Fri Dec 31 1999 19:00:00 GMT-0500 (EST)
Thanks
That is a valid JavaScript Date Object. You can test this by opening a console in Chrome or Firefox, then entering the following:
// Mon Nov 24 2014 09:54:00 GMT-0800 (PST) (looks the same as your example)
console.log( new Date() );
If you want to format the value coming out of moment.js, you can use its format method and a mask.
// Example: November 24th 2014, 09:58:12am
var fdate = moment().format('MMMM Do YYYY, h:mm:ss a');
Moment.js doesn't modify the prototype, it simply wraps it.
If you want to convert a string to a date object using moment.js, you can just call it as such:
moment(your_date); // Unless in UTC mode this will display as local time
In your instance you're using the UTC mode.
2000-01-01T00:00:00.000Z is the GMT date/time.
Using moment.utc("2000-01-01T00:00:00.000Z").toDate() returns this date/time according to your timzone settings.
See : http://www.digitoffee.com/programming/get-local-time-utc-using-moment-js/94/
Hope it helps.