How Can Convert Datetime Form Net To Javascript
Please don't make duplicate vs ASP.NET Parse DateTime result from ajax call to javascript date and Parsing DateTime format passed from Ajax to “dd/MM/yyyy”, I have been read ca
Solution 1:
Look I'm no dates expert but the following looks convincing to me
constparse = str => {
let [_, timestamp, offsetHours, offsetMinutes] = str.match(/(\d+)([+-]\d\d)(\d\d)/).map(Number);
let date = newDate(timestamp);
date.setHours(date.getHours() + offsetHours);
date.setMinutes(date.getMinutes() + offsetMinutes);
return date;
}
console.log(
parse("/Date(1556895600000+0900)/")
.toJSON()
)
console.log(
parse("/Date(1556895600000-0700)/")
.toJSON()
)
Here's a ES3 version in case it doesn't work in older browsers (as the OP said in the comments)
functionparse(str) {
var matches = str.match(/(\d+)([+-]\d\d)(\d\d)/).map(Number);
var timestamp = matches[1];
var offsetHours = matches[2];
var offsetMinutes = matches[3];
var date = newDate(timestamp);
date.setHours(date.getHours() + offsetHours);
date.setMinutes(date.getMinutes() + offsetMinutes);
return date;
}
console.log(
parse("/Date(1556895600000+0900)/")
.toJSON()
)
console.log(
parse("/Date(1556895600000-0700)/")
.toJSON()
)
Post a Comment for "How Can Convert Datetime Form Net To Javascript"