Date time conversion in Javascript from format "/Date(1535515200000)/" [duplicate] - javascript

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);

Related

Convert Date to javascript format [duplicate]

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

JavaScript Date not working properly in IE8 [duplicate]

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());
}

Date object return invalid date in javascript [duplicate]

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.

change this date format to work with new date() [duplicate]

This question already has answers here:
javascript: how to parse a date string
(4 answers)
Closed 8 years ago.
I get a date returned from an API in the format:
var date = result.posted //gives 2014-03-29 02:07:26
when I run
new date(date)
on desktop it works fine
but on mobile, I get the error 'invalid date'
how can I make this work across the board - I want to compare it to the current time ( var cur_time = new date() )
I suggest you to use moment.js, it is very easy to compare date. In your case:
moment("2014-03-29 02:07:26", "YYYY-MM-DD HH:mm:ss").fromNow();
Try this..
var date = new Date(Date.parse(parseInt(result.posted)))
it will give date object like below
Date {Wed Jan 01 2014 05:30:00 GMT+0530 (India Standard Time)}

Converting JSON string into date [duplicate]

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
1351993013​000
which would give you Sunday Nov 04 2012.
in other words:
var jsonDate = parseInt(plugin.versions[0].date, 10) * 1000;

Categories

Resources