I am looking to parse the below date format in JavaScript but I am struggling to make it work reliably.
//Works
var a = new Date("Thu, Nov 7 12:59:45 GMT 2013")
alert(a)
//Invalid date
var b = new Date("Tue, Jun 19 11:14:23 BST 2012")
alert(b)
What is a reliable method of parsing this date format?
I am testing in FireFox 36.
Found that the parsing will automatically associate the timezone if I remove it
var b = new Date("Tue, Jun 19 11:14:23 BST 2012".replace("BST",""))
I would prefer it to just work but meh
Related
I have this problem.
I have this date with this format
var datestring = "2017-10-30T15:03:10.933044Z";
If I write my code like this
var d = new Date(datestring);
I obtaine
Mon Oct 30 2017 16:03:10 GMT+0100 (ora solare Europa occidentale)
because there is one hour of a daylight in italy now. Nevertheless, I would like to have the same hour of 'datestring' (15, and not 16).
Could you help me?
thank you very much
According to ECMA-262, if you want to treat an ISO 8601 format UTC timestamp as local, just remove the Z. However, it will now represent a different moment in time if the local timezone is not GMT+0000.
Also, using the built-in parser is not recommended (see Why does Date.parse give incorrect results?), as some browsers will still treat it as UTC (e.g. Safari 11) or perhaps invalid. You should either write your own function to parse the string, or use a library. There are plenty of good parsing and formatting libraries available.
var s = '2017-10-30T15:03:10.933044Z';
var d = new Date(s.replace(/z/i,''));
console.log(d.toString());
Your input string is in ISO-8601 format. In this format, the Z at the end means the timestamp is UTC-based.
You can obtain a more human-friendly UTC-based string representation with the .toUTCString() method.
var datestring = "2017-10-30T15:03:10.933044Z";
var d = new Date(datestring);
var s = d.toUTCString();
console.log(s) // "Mon, 30 Oct 2017 15:03:10 GMT"
If you want the string in a specific format, then consider using a library like Moment.js.
Why cannot IE parse this string as a Date object.
var d = Date.parse("Fri Jun 11 04:55:12 +0000 2010"); // returns NaN
However, it works well in FireFox. I am running IE 8.
Thanks.
You are getting NaN value in IE 8 and its working in Firefox because the format of the string varies with browser and operating system.
For example, in IE6 for Windows XP, the string is in the following format:
Tue Dec 05 16:47:20 CDT 2006
But in Firefox for Windows XP, the string is
Tue Dec 05 2006 16:47:20 GMT-0500
to make it compatible with both browser you will have to first check the browser in your
javascript code and then accordingly give your input date string.
I've found the jQuery Globalization Plugin date parsing to work best. Other methods had cross-browser issues and stuff like date.js had not been updated in quite a while.
You also don't need a datePicker on the page. You can just call something similar to the example given in the docs:
$.datepicker.parseDate('yy-mm-dd', '2007-01-26');
Is solved my problem by creating an date object and let me give it back the timestamp.
But for this you need to convert you string into this format:
year, month, date, hours, minutes, seconds,ms
an example would be like:
dateObj = new Date(year, month, date);
timestamp = dateObj.getTime();
This works save in IE and FF.
IE Dev Center: Date Object (JavaScript)
Mozilla Dev Network: Date
For your example you would to something like this:
//your string
var str = "Fri Jun 11 04:55:12 +0000 2010";
//maps months to integer from 0 to 11
var monthArray = {"Jan":0, "Feb":1, "Mar":2, "Apr":3, "May":4, "Jun":5, "Jul":6, "Aug":7, "Sep":8, "Oct":9, "Nev":10, "Dec":11};
//get the values from the string
var regex = /^[^ ]+ ([^ ]+) (\d{1,2}) (\d{2}):(\d{2}):(\d{2}) \+(\d{4}) (\d{4})$/;
match = regex.exec(str);
var month = monthArray[match[1]],
date = match[2],
hours = match[3],
minutes = match[4],
seconds = match[5],
ms = match[6],
year = match[7];
//create date object with values
var dateObject = new Date(year, month, date, hours, minutes , seconds, ms);
var ts = dateObject.getTime(); //timestamp in ms
Problem
In case your date is stored in SQL datetime like 2020-04-07 05:30:00 and want to parse it in IE. When you parse it with JavaScript in IE using new Date(), it outputs Invalid Date while latest versions of Chrome and Firefox parse this date correctly.
Solution
You have to replace <space> with T in datetime string coming from SQL.
Example
let myDate = '2020-04-07 05:30:00';
let myFormattedDate = myDate.replace(' ', 'T'); // '2020-04-07T05:30:00'
console.log(new Date(myFormattedDate));
because of the +00000. try to add that the last
var d = Date.parse("Fri Jun 11 04:55:12 2010 +0000");
This may help you. I just solved a problem similar to this.
Problem with Javascript Date function in IE 7, returns NaN
{"date":"Thu Dec 06 14:56:01 IST 2012"}
I am getting this string as JSON can I convert it to JS date object?
Edit: Unfortunately i was totally wrong, sry for that,my bad, it happened to always result in today,
but to not screw you up, heres an solution which should work for you anyway
If you get Different Time strings from your Server, maybe the best way is to write a Regex pattern that matches your String patterns
Access your date propertie from your JSON Object
Since instantiating a Date object with this "Thu Dec 06 14:56:01 IST 2012" String would result in an Invalid Date
Remove the "IST" myJson.date.replace(" IST","")
Instantiate the your Date object with your new String myDate = new Date("Thu Dec 06 14:56:01 2012")
Now theres really your Date Object
var myJson = {"date":"Thu Dec 06 14:56:01 IST 2012"}
var myDate = new Date(myJson.date.replace(" IST",""))
console.log(myDate.toLocaleDateString())
Heres the JSBin
The right way to convert your JSON to the data object it's parsing this date as a string.
var myJson = {"date":"Thu Dec 06 14:56:01 IST 2013"}
var myDate = new Date(Date(myJson.date))
console.log(myDate.getFullYear()) // 2012
Doesn't work with a Year different from the current one.
Related link
Where can I find documentation on formatting a date in JavaScript?
I getting a Date value from JavaScript to a controller in MVC and I would like to parse it to .NET format DateTime but its giving me an error such as:
The string was not recognized as a valid DateTime.
The Format of the JavaScript date is:
"Wed May 23 2012 01:40:00 GMT+0200 (W. Europe Daylight Time)"
I've tried this but its not working:
DateTime.ParseExact(begin.Substring(1, 24), "ddd MMM d yyyy HH:mm:ss", CultureInfo.InvariantCulture);
anyone can give me a sample code please? thanks!
The following parses well with the default DateTime modelbinder in a .NET MVC Controller:
var myJsDate = new Date();
var myDotNetDate = myJsDate.toISOString();
Instead of parsing a textual representation it would be more robust to construct a DateTime from a timestamp instead. To get a timestamp from a JS Date:
var msec = date.getTime();
And to convert msec (which represents a quantity of milliseconds) into a DateTime:
var date = new DateTime(1970, 1, 1, 0, 0, 0, 0); // epoch start
date = date.AddMilliseconds(msec); // you have to get this from JS of course
Here is what I did and why. I hope this Helps.
JS Date var d = new Date()
returns: Thu Nov 19 08:30:18 PST 2015
C# does not like this format so convert it to UTC:
var dc = d.toUTCString()
returns: Thu, 19 Nov 2015 16:30:18 UTC
UTC – The Worlds Time Standard is not a time zone so you need to change it to a time zone
var cr = dc.replace("UTC","GMT")
now it is ready to
Thu, 19 Nov 2015 16:30:18 GMT
In one line
var ol = d.toUTCString().replace("UTC","GMT")`
Thu, 19 Nov 2015 16:30:18 GMT
for C#
DateTime DateCreated= DateTime.Parse(ol);
You don't need any conversion: the default DateTime modelbinder in a .NET MVC Controller works fine with a JavaScript Date object.
Using Moment.js
1) .NET DateTime -> JavaScript Date
var jsDate = moment(dotNetDateTime).toDate();
2) JavaScript Date -> .NET DateTime
var dotNetDateTime = jsDate;
I am using date function in javascript to get the currentDate.
as
var currentTime =new Date()
But it is giving me different response in IE and Firefox as
Consider for today's Date
IE - Tue 24 Apr 17:05:22 UTC 0530 2012
Mozilla - Tue 24 Apr 17:05:22 2012 GMT 0530
The Problem is that I Want to convert today's date which is in "String" to "Date" into format - EEE mmm-dd hh:mm:ss IST yyyy
But as i m getting different response in this browser I m not getting how to apply simpleDateformatter to convert current date and time to the suitable format .
You can use functions of object Date. For example:
var currentTime = new Date();
var date = currentTime.getDate();
var day = currentTime.getDay();
var hours = currentTime.getHours();
With help this functions you can convert current date and time to the suitable format.