SoFunction
Updated on 2025-03-10

JS implements a method to convert DateTime Json type to standard time

This article describes the JS implementation method of converting the DateTime Json type to standard time. Share it for your reference, as follows:

Let's take the example directly, as follows:

onload = function () {
  var thisDateText = '/Date(1401076829)/';
  (getLocalTime(thisDateText));
};
function getLocalTime(dateText) {
  dateText = ("/Date(", "").replace(")/", "");
  /*
     What should be noted is:
       Don't pass Date in the string (such characters are also passed in, you have to process it first, so it is very convenient to handle it.
     You can use the replace method
     For example: replace("/Date(","").replace(")/","");
   */
  //Return on May 26, 2014 at 12:00 pm  //return new Date(parseInt(dateText) * 1000).toLocaleString().replace(/:\d{1,2}$/, ' ');
  //Return on May 26, 2014 at 12:0 pm  //return new Date(parseInt(dateText) * 1000).toLocaleString().substr(0, 17);
  //Return 2014-5-26 12:00:29  return new Date(parseInt(dateText) * 1000).toLocaleString().replace(/Year|moon/g, "-").replace(/day/g, " ").replace(/morning/g, " ").replace(/afternoon/g, " ");
}

PS: Friends who are interested in the display of JavaScript time and dates can also refer to the online tools of this site:

Unix timestamp conversion tool:
http://tools./code/unixtime

Online time inquiry around the world:
http://tools./zhuanhuanqi/worldtime

Online Perpetual Calendar:
http://tools./bianmin/wannianli

Web Perpetual Calendar:
http://tools./bianmin/webwannianli

For more information about JavaScript, please view the special topic of this site: "Summary of JavaScript time and date operation skills》、《Summary of JavaScript switching effects and techniques》、《Summary of JavaScript search algorithm skills》、《Summary of JavaScript animation special effects and techniques》、《Summary of JavaScript Errors and Debugging Skills》、《Summary of JavaScript data structure and algorithm techniques》、《JavaScript traversal algorithm and skills summary"and"Summary of JavaScript mathematical operations usage

I hope this article will be helpful to everyone's JavaScript programming.