使用Javascript的当前时间格式

使用Javascript的当前时间格式,第1张

使用Javascript的当前时间格式

Javascript Date有几种方法可以让您提取其部分:

getFullYear()
-返回4位数字的年份
getMonth()
-返回表示该年月份的从零开始的整数(0-11)。
getDate()
-返回月份中的一天(1-31)。
getDay()
-返回星期几(0-6)。0是星期日,6是星期六。
getHours()
-返回一天中的小时(0-23)。
getMinutes()
-返回分钟(0-59)。
getSeconds()
-返回第二个(0-59)。
getMilliseconds()
-返回毫秒数(0-999)。
getTimezoneOffset()
-返回机器本地时间和UTC之间的分钟数。

没有内置方法可让您获取本地化的字符串,例如“星期五”,“二月”或“
PM”。您必须自己编写代码。要获取所需的字符串,您至少需要存储几天和几个月的字符串表示形式:

var months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];var days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];

然后,使用上述方法将其放在一起:

var d = new Date();var day = days[d.getDay()];var hr = d.getHours();var min = d.getMinutes();if (min < 10) {    min = "0" + min;}var ampm = "am";if( hr > 12 ) {    hr -= 12;    ampm = "pm";}var date = d.getDate();var month = months[d.getMonth()];var year = d.getFullYear();var x = document.getElementById("time");x.innerHTML = day + " " + hr + ":" + min + ampm + " " + date + " " + month + " " + year;

我有一个喜欢包含在标准库中的日期格式功能。它采用定义所需输出的格式字符串参数。格式字符串 大致
基于.Net自定义日期和时间格式字符串。对于您指定的格式,以下格式字符串将起作用:

"dddd h:mmtt d MMM yyyy"

var d = new Date();var x = document.getElementById("time");x.innerHTML = formatDate(d, "dddd h:mmtt d MMM yyyy");

这是我的完整日期格式化功能:

function formatDate(date, format, utc) {    var MMMM = ["x00", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];    var MMM = ["x01", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];    var dddd = ["x02", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];    var ddd = ["x03", "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];    function ii(i, len) {        var s = i + "";        len = len || 2;        while (s.length < len) s = "0" + s;        return s;    }    var y = utc ? date.getUTCFullYear() : date.getFullYear();    format = format.replace(/(^|[^\])yyyy+/g, "" + y);    format = format.replace(/(^|[^\])yy/g, "" + y.toString().substr(2, 2));    format = format.replace(/(^|[^\])y/g, "" + y);    var M = (utc ? date.getUTCMonth() : date.getMonth()) + 1;    format = format.replace(/(^|[^\])MMMM+/g, "" + MMMM[0]);    format = format.replace(/(^|[^\])MMM/g, "" + MMM[0]);    format = format.replace(/(^|[^\])MM/g, "" + ii(M));    format = format.replace(/(^|[^\])M/g, "" + M);    var d = utc ? date.getUTCDate() : date.getDate();    format = format.replace(/(^|[^\])dddd+/g, "" + dddd[0]);    format = format.replace(/(^|[^\])ddd/g, "" + ddd[0]);    format = format.replace(/(^|[^\])dd/g, "" + ii(d));    format = format.replace(/(^|[^\])d/g, "" + d);    var H = utc ? date.getUTCHours() : date.getHours();    format = format.replace(/(^|[^\])HH+/g, "" + ii(H));    format = format.replace(/(^|[^\])H/g, "" + H);    var h = H > 12 ? H - 12 : H == 0 ? 12 : H;    format = format.replace(/(^|[^\])hh+/g, "" + ii(h));    format = format.replace(/(^|[^\])h/g, "" + h);    var m = utc ? date.getUTCMinutes() : date.getMinutes();    format = format.replace(/(^|[^\])mm+/g, "" + ii(m));    format = format.replace(/(^|[^\])m/g, "" + m);    var s = utc ? date.getUTCSeconds() : date.getSeconds();    format = format.replace(/(^|[^\])ss+/g, "" + ii(s));    format = format.replace(/(^|[^\])s/g, "" + s);    var f = utc ? date.getUTCMilliseconds() : date.getMilliseconds();    format = format.replace(/(^|[^\])fff+/g, "" + ii(f, 3));    f = Math.round(f / 10);    format = format.replace(/(^|[^\])ff/g, "" + ii(f));    f = Math.round(f / 10);    format = format.replace(/(^|[^\])f/g, "" + f);    var T = H < 12 ? "AM" : "PM";    format = format.replace(/(^|[^\])TT+/g, "" + T);    format = format.replace(/(^|[^\])T/g, "" + T.charAt(0));    var t = T.toLowerCase();    format = format.replace(/(^|[^\])tt+/g, "" + t);    format = format.replace(/(^|[^\])t/g, "" + t.charAt(0));    var tz = -date.getTimezoneOffset();    var K = utc || !tz ? "Z" : tz > 0 ? "+" : "-";    if (!utc) {        tz = Math.abs(tz);        var tzHrs = Math.floor(tz / 60);        var tzMin = tz % 60;        K += ii(tzHrs) + ":" + ii(tzMin);    }    format = format.replace(/(^|[^\])K/g, "" + K);    var day = (utc ? date.getUTCDay() : date.getDay()) + 1;    format = format.replace(new RegExp(dddd[0], "g"), dddd[day]);    format = format.replace(new RegExp(ddd[0], "g"), ddd[day]);    format = format.replace(new RegExp(MMMM[0], "g"), MMMM[M]);    format = format.replace(new RegExp(MMM[0], "g"), MMM[M]);    format = format.replace(/\(.)/g, "");    return format;};


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5041433.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-15
下一篇 2022-11-15

发表评论

登录后才能评论

评论列表(0条)

保存