Home > Article > Web Front-end > JS formatting Json date case sharing
After 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); }
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; }
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).
If you have other methods, please share them.
Please explain when reprinting.
Related recommendations:
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!