前端开发日期格式化Date.format

前端开发和app开发工具
前端语言移动开发工具
前端团队开发工具

html 代码

<!DOCTYPE html>
<html lang=”zh-CN”>
    <head>
        <meta charset=”UTF-8″ />
        <meta name=”Keywords” content=”” />
        <meta name=”Description” content=”” />
        <meta http-equiv=”X-UA-Compatible” content=”IE=edge,chrome=1″ />
        <meta
            name=”viewport”
            content=”width=device-width,initial-scale=1,user-scalable=no”
        />
        <meta name=”apple-mobile-web-app-capable” content=”yes” />
        <meta content=”telephone=no” name=”format-detection” />
        <meta content=”email=no” name=”format-detection” />
        <title>Date.format</title>
    </head>
    <body>
        <script>
            Date.format = function(format, date) {
                date instanceof Date || (date = new Date(date))
                var f = {
                        ‘y+’: date.getFullYear(),
                        ‘M+’: date.getMonth() + 1,
                        ‘d+’: date.getDate(),
                        ‘h+’: date.getHours(),
                        ‘m+’: date.getMinutes(),
                        ‘s+’: date.getSeconds(),
                    },
                    fd
                for (var o in f) {
                    if (new RegExp(o).test(format)) {
                        fd = RegExp.lastMatch
                        format = format.replace(
                            fd,
                            o === ‘y+’
                                ? f[o].toString().slice(-fd.length)
                                : fd.length === 1
                                ? f[o]
                                : (f[o] + 100).toString().slice(-fd.length),
                        )
                    }
                }
                return format
            }
            var timestamp = Date.now() //时间戳
            alert(Date.format(‘yyyy-MM-dd hh:mm:s’, timestamp))
        </script>
    </body>
</html>
linux上的前端开发工具
2014年度最佳web前端开发工具和框架
前端开发工具 记事本
» 本文来自:前端开发者 » 《前端开发日期格式化Date.format》
» 本文链接地址:https://www.rokub.com/6371.html
» 您也可以订阅本站:https://www.rokub.com
赞(0)
64K

评论 抢沙发

评论前必须登录!