Home > Back-end >  Convert asp.net date format using JavaScript or jQuery
Convert asp.net date format using JavaScript or jQuery

Time:10-27

dateCreated = Convert.ToDateTime(rd["dateCreated"].ToString() --CONTROLLER

public DateTime dateCreated { get; set; } --Model

/Date(1666677654000)/ ---> this is what I get

I am displaying this in my HTML table using ajax method get.

Is there anyway to convert this using JavaScript?

CodePudding user response:

You can get a JS date object by using eval

var dateStr = "/Date(1666677654000)/";
var date = eval("new "   dateStr.replace(/\//g, ""));
console.log(date.toString())

result:

enter image description here

Update

Thanks to @JavaScript for providing another way.

var dateStr = "/Date(1666677654000)/";
var date = new Date(parseInt("/Date(1666677654000)/".substr(6)));
console.log(date.toString())

.substr(6) is undefine length, so it return the string from 6th(start) to the end of the string.

.substr(start), start: The index of the first character to include in the returned substring.

If length is omitted or undefined, substr() extracts characters to the end of the string.

Read .substr(start) to know more.

  • Related