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

Date() date format conversion example code

零下一度
Release: 2017-06-26 10:20:04
Original
1512 people have browsed it

Convert to Date() default format

var parserDate = function (date) {

var t = Date.parse(date);

if (!isNaN(t)) {

return new Date(Date.parse(date.replace(/-/g, "/")));

} else {

Return New Date ();

##}

}

##//Sat Jun 10 2017 13:59:46 GMT+0800 (China Standard Time)

Convert to common format

var formatDate = function (date) { var y = date.getFullYear();

var m = date.getMonth() + 1;

m = m < 10 ? ( '0' + m) : m;

var d = date.getDate();

d = d < 10 ? ('0' + d) : d;

var h = date.getHours();

h = h< 10 ? ('0' + h) : h;

var minute = date.getMinutes();

minute = minute < 10 ? ('0' + minute) : minute;

var second=date.getSeconds();

second=second<10?( '0'+second):second;

return y + '-' + m + '-' + d+' '+h+':'+minute+':'+second;

};

var a=new Date();

formatDate (a);

//"2017-06-10 14:02:27"

The above is the detailed content of Date() date format conversion example code. 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!