Date的格式转换

1.toLocaleString()  根据本地时间把 Date 对象转换为字符串:

var d = new Date();
d.toLocaleString();
//"2018/8/21 下午3:01:29" 

 


  :toLocaleString()的其他用法:

    1.把数组转化成以逗号隔开的字符串;

    2.数字每三位以逗号隔开;

   var arr = [1,2,3];
    arr.toLocaleString();
    //"1,2,3"

    var num = 87635.234;
    num.toLocaleString();
    //"87,635.234"

2.toLocaleDateString() 根据本地时间格式,把 Date 对象的日期部分转换为字符串。

var d = new Date();
d.toLocaleDateString();
//"2018/8/21"

3.toLocaleTimeString() 根据本地时间格式,把 Date 对象的时间部分转换为字符串。

var d = new Date();
d.toLocaleTimeString();
//"下午3:00:57"

4.toString() 把 Date 对象转换为字符串:

var d = new Date();
d.toString();
//"Tue Aug 21 2018 15:18:50 GMT+0800 (中国标准时间)"

5.toDateString() 把 Date 对象的日期部分转换为字符串:

var d = new Date();
d.toDateString();
//"Tue Aug 21 2018"

6.toTimeString() 把 Date 对象的时间部分转换为字符串:

var d = new Date();
d.toTimeString();
//15:13:58 GMT+0800 (中国标准时间)

posted on 2018-08-21 15:35  香米发霉了  阅读(356)  评论(0编辑  收藏  举报

导航