javascript如何转换时间


本文摘自PHP中文网,作者coldplay.xixi,侵删。

javascript转换时间的方法:创建一个【util.js】文件,在里面重新封装一下Date的format方法,代码为【let date = new Date().Format("yyyy年M月dd日\tEEE\tHH:mm:ss")】。

本教程操作环境:windows7系统、javascript1.8.5版,DELL G3电脑。

javascript转换时间的方法:

创建一个util.js文件,在里面重新封装一下Date的format方法:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

//Date的prototype 属性可以向对象添加属性和方法。  

Date.prototype.Format = function(fmt) {

var o = {

    "M+": this.getMonth() + 1, //月份        

    "d+": this.getDate(), //日        

    "h+": this.getHours() % 12 === 0 ? 12 : this.getHours() % 12, //小时        

    "H+": this.getHours(), //小时        

    "m+": this.getMinutes(), //分        

    "s+": this.getSeconds(), //秒        

    "q+": Math.floor((this.getMonth() + 3) / 3), //季度        

    "S": this.getMilliseconds() //毫秒        

};

var week = {

    "0": "\u65e5",

    "1": "\u4e00",

    "2": "\u4e8c",

    "3": "\u4e09",

    "4": "\u56db",

    "5": "\u4e94",

    "6": "\u516d"

};

if (/(y+)/.test(fmt)) {

    fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));

}

if (/(E+)/.test(fmt)) {

    fmt = fmt.replace(RegExp.$1, ((RegExp.$1.length > 1) ? (RegExp.$1.length > 2 ? "\u661f\u671f" : "\u5468") : "") + week[this.getDay() + ""]);

}

for (var k in o) {

    if (new RegExp("(" + k + ")").test(fmt)) {

        fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));

    }

}

return fmt;

};

具体调用的时候就可以变得很方便,想要什么格式就可以形成什么格式,例如 年月日 星期几 时分秒,做个简单的例子,如果有哪里看不明白可以评论,我看见会给你解释的。

1

2

let date = new Date().Format("yyyy年MM月dd日\tEEE\tHH:mm:ss");

        $("#date-now").html(date);

相关免费学习推荐:javascript学习教程

以上就是javascript如何转换时间的详细内容,更多文章请关注木庄网络博客

相关阅读 >>

javascript怎么删除表格行

什么是javascript的事件

javascript怎么将array转为字符串

jsp写javascript用什么标签

javascript怎么验证是否为数字

vue导出excel遇到的问题解决方法

react高阶组件(装饰器)的介绍(代码示例)

怎么检查javascript语法错误

深入浅析javascript中的快速排序

javascript怎么隐藏鼠标

更多相关阅读请进入《javascript》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...