Home > Web Front-end > JS Tutorial > body text

JS formatting Json date case sharing

php是最好的语言
Release: 2018-08-10 09:51:06
Original
1735 people have browsed it

JS formatting Json date case sharingAfter formatting in the background, it becomes a string. I don’t know how to set it in, so I still choose to format it in the foreground.

Formatting Date

/**
 * 格式化日期
 * @param obj
 * @returns 形如 2018-08-09
 */function fmtDate(obj){    var date =  new Date(obj);    var y = 1900+date.getYear();    var m = "0"+(date.getMonth()+1);    var d = "0"+date.getDate();    return y+"-"+m.substring(m.length-2,m.length)+"-"+d.substring(d.length-2,d.length);
}
Copy after login

Similar to formatting date, you can use this method to format time

/**
 * 格式化时间
 * @param obj
 * @returns 形如2018-08-09 08:00:00
 */function fmtDateTime(obj){    var d=new Date(obj);      var year=d.getFullYear();      var month="0"+(d.getMonth()+1);      var day="0"+d.getDate();      var hour="0"+d.getHours();      var minute="0"+d.getMinutes();      var second="0"+d.getSeconds();      var time=year+'-'+month.substring(month.length-2,month.length)+'-'+day.substring(day.length-2,day.length)+      ' '+hour.substring(hour.length-2,hour.length)+':'+minute.substring(minute.length-2,minute.length)+':'+
      second.substring(second.length-2,second.length);      return time;
}
Copy after login

It took me a long time to figure this out. In fact, it's not difficult. Maybe I'm not familiar with JS. There are always various questions found online. There are many js files that need to be imported, but it still doesn't work. I'm tired. If you need these js files, you can contact me. (I don’t need to introduce other js for this).
JS formatting Json date case sharing

If you have other methods, please share them.
Please explain when reprinting.

Related recommendations:

Json operation date format

js processing Json time with T time format (practical practice)

The above is the detailed content of JS formatting Json date case sharing. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!