js格式化时间

js怎么格式化时间呢?不知道的小伙伴来看看小编今天的分享吧!

js怎么格式化时间有三种格式:

格式一:2018-1-29-10:34:49

var curr_time = new Date();

Myformatter(curr_time);

function myformatter(date){

var strDate = date.getFullYear()+"-";

strDate += date.getMonth()+1+"-";

strDate += date.getDate()+"-";

strDate += date.getHours()+":";

strDate += date.getMinutes()+":";

strDate += date.getSeconds();

alert("strDate:"+strDate);

return strDate ;

}

格式二: 2018-1-29

function myformatter(date){

var strDate = date.getFullYear()+"-";

strDate += date.getMonth()+1+"-";

strDate += date.getDate();

alert("strDate:"+strDate);

return strDate ;

}

格式三:2018-02-05 (月份和日期小于10的时候,在前面自动加零)

function myformatter(date){

var strDate = date.getFullYear()+"-";

if(date.getMonth()<10){

var s = date.getMonth()+1+"-";

strDate += "0"+s;

}else{

strDate += date.getMonth()+1+"-";

}

if(date.getDate()<10){

strDate += "0"+date.getDate();

}else{

strDate += date.getDate();

}

return strDate ;

}

以上就是小编今天的分享了,希望可以帮助到大家。

标签:js 格式化时间

免责声明:本内容来自橡树街平台创作者或收集于互联网公开资源,不代表橡树街网的观点和立场。如有侵权内容,请联系我们删除。联系邮箱:ihuangque@qq.com
相关推荐
  • js防水涂料是什么
    js防水涂料是什么
    07-29
  • jspp是什么软件
    jspp是什么软件
    07-28
  • js lastindexof
    js lastindexof
    08-05
  • js 获取url参数
    js 获取url参数
    08-05
  • js正则表达式用法
    js正则表达式用法
    08-05
  • js刷新当前页面
    js刷新当前页面
    08-05
  • js endswith
    js endswith
    08-05
  • js设置style
    js设置style
    08-05
  • js保留两位小数四舍五入
    js保留两位小数四舍五入
    08-05
  • js格式化时间
    js格式化时间
    08-05