Working with datetime in Javascript(apache/php/js project) and making alert for debugging I get message like "Sun Nov 30 2014 02:00:00 GMT+0200 ",
though in var time was not specified at all. That is gvfery confusing...
Which is the best and safe way to get rid of this GMT 2 hours ?
Use .toISOString(), e.g:
var myDate = new Date().toISOString();
For older browsers, which don't natively support this method, the function definition can be found here: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Date/toISOString
Related
I'm trying to achieve this date format..
I'm getting my date from back-end, but its in server time. It looks like this
var d.date = "2017-04-26 07:49:09"
Then, I'm formatting it so date constructor would accept the format properly
var dateFormated = d.date.replace(' ', 'T');
Then, out of it I'm constructing my date object as follows:
var dateplublished = new Date(dateFormated);
Which gives me the following result :
Wed Apr 26 2017 07:49:09 GMT+0300 (Eastern Europe Daylight Time)
Everything seems to be fine, except that the GMT +0300 won't add. It supposed to spit out :
Wed Apr 26 2017 10:49:09 GMT+0300 (Eastern Europe Daylight Time)
How can I achieve this, so it Would be supported in all major browsers including ios safari etc? I managed to do it before, by doing it like so :
var dateFormated = d.date;
var dateplublished = new Date(dateFormated + " " + "UTC");
This way it would spit out everything perfectly, except that it would show NaN-NaN-NaN on ios safari/chrome.
You can all Z at the end of date. That way new Date(2017-04-26T07:49:09Z); will be resolved to your local time.
Date parsing is best with momentjs.
It's much more reliable and provides cross-browser support.
Then you can use this to achieve what you're trying to do: https://momentjs.com/docs/#/manipulating/local/
How can I create a Date object from a date with this format:
03/23/2016 02:00:00 PM
The Date object can parse strings: new Date('03/23/2016 02:00:00 PM')
For example:
var date = new Date('03/23/2016 02:00:00 PM') // => "Wed Mar 23 2016 14:00:00 GMT-0700 (PDT)"
date.getFullYear() // => 2016
However, I would recommend using a library that someone else has already spent time considering the edge cases, like time zones, etc. (a good one I've used is moment.js).
Keep in mind (from the moment.js docs):
Warning: Browser support for parsing strings is inconsistent. Because there is no specification on which formats should be supported, what works in some browsers will not work in other browsers.
For consistent results parsing anything other than ISO 8601 strings, you should use String + Format.
var date = new Date("3/23/2016 02:00:00 PM");
console.log(date);
You can then access all the methods of the Date object.
Looking at MDN
var date = new Date('03/23/2016 02:00:00 PM')
You can use something like date.js:
First use script, then write date:
<script type="text/javascript" src="http://www.datejs.com/build/date.js"></script>
....
document.write(new Date().toString("dd:MM:yyyy hh:mm:ss tt"));
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.
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!
First try is in IE 9 console:
new Date('2013-10-24T07:32:53')
Thu Oct 24 07:32:53 UTC+0200 2013
returns as expected
Next try is in FireFox 24 console:
new Date('2013-10-24T07:32:53')
Date {Thu Oct 24 2013 07:32:53 GMT+0200 (Central Europe Standard Time)}
Then I go into Chrome 30 console:
new Date('2013-10-24T07:32:53')
Thu Oct 24 2013 09:32:53 GMT+0200 (Central Europe Daylight Time)
But the time is 09 here, it should be 07.
Is this a bug in chrome or am I doing something wrong here?
I can't use any other format than this '2013-10-24T07:32:53' that I get by JSON from C#.
I need to get the hour of this timestamp, with the getHours I get the incorect value in Chrome.
Solution:
var inputHour = input.split('T')[1];
inputHour = inputHour.substring(0, 2);
Its no bug. The implementation of date parse function differs across browsers & so does the format of the dateString accepted by it.
However this format seems to work same across ... link:
new Date("October 13, 1975 11:13:00")
If possible, try and use
new Date(year, month, day, hours, minutes, seconds, milliseconds)
for guaranteed results.
Regarding your format try parsing it yourself. Something like :
var str = '2013-10-24T07:32:53'.split("T");
var date = str[0].split("-");
var time = str[1].split(":");
var myDate = new Date(date[0], date[1]-1, date[2], time[0], time[1], time[2], 0);
Note (Thanks to RobG for this) : The Date constructor used above expects month as 0 - 11 & since October is 10 as per date String, the month has to be modified before passing it to the constructor.
Reference.
See this thread:
Why does Date.parse give incorrect results?
It looks like the behavior of the parsing signature of the Date constructor is completely implementation dependent.
Given:
var s = '2013-10-24T07:32:53';
in ES5 compliant browsers you could do:
var d = new Date(s + 'Z');
but for compatibility across all browsers in use, better to use (assuming date is UTC):
function dateFromString(s) {
s = s.split(/\D/);
return new Date(Date.UTC(s[0],--s[1],s[2],s[3],s[4],s[5]));
}