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的module模式编程

javascript用什么分割成数组

canvas波浪效果的实现代码

js中闭包的概念

父元素<a>标签的默认行为以及click事件之间的相互影响

javascript基础入门买什么书

canvas中普通动效与粒子动效的实现 方法介绍(代码示例)

javascript如何检测网络

node批量下载文件到本地的方法介绍(附代码)

怎样在javascript添加图片

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




打赏

取消

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

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

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

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

评论

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