javascript时间转换的方法


本文摘自PHP中文网,作者藏色散人,侵删。

javascript时间转换的方法:首先创建一个util.js文件;然后在里面重新封装一下Date的format方法;最后通过“new Date().Format()”方式调用实现时间转换即可。

本文操作环境: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;

};

阅读剩余部分

相关阅读 >>

javascript数组有哪些方法

详解javascript中的padstart()和padend()方法

javascript 作用域详解

window对象在前端领域的角色

javascript自定义函数的写法是什么

javascript中的arguments对象的用法介绍

es6中generator函数的用法介绍

javascript事件委托的详细介绍(附代码)

聊聊javascript中实现抽离公共函数的方法

javascript中如何实现异步编程模式

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




打赏

取消

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

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

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

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

评论

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