This question already has answers here:
Javascript JSON Date parse in IE7/IE8 returns NaN
(5 answers)
Closed 7 years ago.
Chrome showing result as expected but IE-8 giving NAN when i execute the following:
Chrome:
d = new Date("2014 12 01") // results Mon Dec 01 2014 00:00:00 GMT+0500 (Pakistan Standard Time)
IE-8:
d = new Date("2014 12 01") // results NaN undefined
The format you're trying to parse doesn't match the only specific format that new Date is required to parse. To parse it reliably cross-browser, you need to parse it explicitly — either in your own code, which can be trivially done with a regex, or using a library like MomentJS and telling it what the format is.
The trivial regex solution:
// NOTE! Uses local time.
var yourString = "2014 12 01";
var parts = yourString.match(/^(\d{4}) (\d{2}) (\d{2})$/);
if (parts) {
var date = new Date(+parts[1], +parts[2] - 1, +parts[3]);
alert(date.toString());
}
Related
This question already has answers here:
Why does Date.parse give incorrect results?
(11 answers)
Parsing a string to a date in JavaScript
(35 answers)
Closed 2 years ago.
I am trying to convert this date into Mon Nov 26 2018 10:32:04 GMT (I am getting this data from the Api so i can't make changes to it)
I assume it is considering 26 as months thats why it is showing it as invalid date
Can Anyone help me with this. How to convert that date into the expected output i specified.
How to get
var d = new Date("26-11-2018 10:32:04")
return d; //Error: Invalid Date
expected Output: Mon Nov 26 2018 10:32:04 (IST)
Use moment.js to parse the date.
moment("26-11-2018 10:32:04", "DD-MM-YYYY HH-mm-ss").toDate()
Alternatively, if you really don't want to use moment for whatever reason, you can use regex magic.
new Date("26-11-2018 10:32:04".replace(/^(\d+)-(\d+)-(\d+) (\d+):(\d+):(\d+)$/, "$3-$2-$1T$4:$5:$6Z"))
This is not a robust as #Yevgen answer but it also much simpler.
All I'm doing is removing the - and flipping the day and month values
const items = "26-11-2018 10:32:04".split('-')
new Date(`${items[1]} ${items[0]} ${items[2]}`)
This works for personal projects but I highly recommend using moment.js
This question already has answers here:
Parsing a Auto-Generated .NET Date Object with Javascript/JQuery
(2 answers)
Closed 4 years ago.
Hi All this is mostly asked question still i didn't find easy and direct way to convert this in to date format.
current format is in
"/Date(1535515200000)/"
if i executed the below line that will give the needed date format. Is there any direct way to get the date format from "/Date(1535515200000)/" to Wed Aug 29 2018 00:00:00 GMT-0400 (Eastern Daylight Time)
new Date(1535515200000)
var s = "/Date(1535515200000)/";
var ts = s.substring(s.indexOf('(')+1,s.lastIndexOf(')'));
console.log(new Date(Number(ts)).toISOString());
Would print "2018-08-29T04:00:00.000Z"
If you are doing it from JSON.parse, you can use the reviver parameter and parse it out.
const x = JSON.parse('{"foo": "/Date(1535515200000)/"}', (key, value) =>
typeof value === 'string' && value.startsWith("/Date(")
? new Date(+value.match(/(\d+)/)[0]) // return new date
: value // return everything else unchanged
);
console.log(x);
This question already has answers here:
Parsing a string to a date in JavaScript
(35 answers)
Why does Date.parse give incorrect results?
(11 answers)
Closed 5 years ago.
When using the below line I am expecting the result to be '22/09/2017 14:05:43', however it is actually returning '09/10/2018 14:05:43'.
var theDate = new Date('22/09/2017 14:05:43').toLocaleString();
I know there are js libraries out there such as moment.js that can be used for a lot of date time manipulation, but I was just wondering if anyone knew why this was happening and how I can get this to return the expected date?
Parsing of date strings is mostly implementation dependent, so it's generally recommended to avoid the built-in parser. Most browsers treat JavaScript dates with a pattern xx/xx/xxxx as MM/DD/YYYY, so 22/09/2017 is seen as either an invalid date (e.g. Safari, Firefox, Chrome), or the 9th day of the 22nd month of 2017 (apparently your browser).
Your browser is interpreting it as 'the 9th day of the 22nd month', so you're ending up on October 9th 2018, 22 months and 9 days into 2017.
To resolve this, you can separate the string into parts and give them to the constructor, avoiding the built-in parser (remembering to subtract 1 from the month number):
new Date(2017, 8, 22, ...)
Refer to MDN's Date documentation for more information.
Well the initial date string you passed to the Date constructor is wrong, it should be in the format 'MM/DD/YYYY HH:mm:ss'.
Your actual code will give Invalid Date:
var theDate = new Date('22/09/2017 14:05:43');
console.log(theDate);
var str = theDate.toLocaleString();
console.log(str);
Here 22 will be treated as a month and 09 will be treated as day. You need to fix it so it follows the Date standards:
var theDate = new Date('09/22/2017 14:05:43');
console.log(theDate);
var str = theDate.toLocaleString();
console.log(str);
This question already has answers here:
new Date() works differently in Chrome and Firefox
(5 answers)
Closed 8 years ago.
I have a date like "2015-05-01 09:00:00" in chrome & opera returns the format which i need "Fri May 01 2015 09:00:00 GMT+0530 (India Standard Time)" using the code
var austDay = new Date('2015-05-01 09:00:00');
but in firefox and explorer it returns invalid date
If you want consistency, you should manually parse date strings. If you want "2015-05-01 09:00:00" to be treated as UTC, then:
function parseISOUTC(s) {
var b = s.split(/\D/);
var d = new Date(Date.UTC(b[0], --b[1], b[2]||1, b[3]||0, b[4]||0, b[5]||0));
return d && d.getMonth() == b[1]? d : NaN;
}
Note that if the date values are invalid (e.g. 2015-02-30) then it will return NaN. You can add validation of time values if you want. Missing values are treated per ECMA-262. Note that 2 digit years will be treated as 20th century.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Convert a Unix timestamp to time in Javascript
I'm trying to convert the string that this api returns for date. The format is 1351993013. I tried the following line of JS but the date is completely wrong.
var jsonDate = plugin.versions[0].date;
var pluginDate = new Date(jsonDate);
Which returns:
Fri Jan 16 1970
This is the first time I've tried to format a JSON date so it's a bit confusing. Can anyone help?
That would be seconds, and javascript uses milliseconds which would be
1351993013000
which would give you Sunday Nov 04 2012.
in other words:
var jsonDate = parseInt(plugin.versions[0].date, 10) * 1000;