Javascript 在javascript中将日期转换为特定格式?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5637576/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert date to specific format in javascript?
提问by harishtps
Hi i want to convert the current date to a format as follows in javascript: Apr 12, 2011 06:42:03.
嗨,我想在 javascript 中将当前日期转换为如下格式:2011 年 4 月 12 日 06:42:03。
Any suggestions?????
有什么建议?????
回答by clamchoda
Little example I just whipped up for you. Very easy to tell whats going on.
我刚刚为你准备的小例子。很容易说出发生了什么。
var monthNames = new Array("January", "February", "March",
"April", "May", "June", "July", "August", "September",
"October", "November", "December");
var today = new Date();
var cDate = today.getDate();
var cMonth = today.getMonth();
var cYear = today.getFullYear();
var cHour = today.getHours();
var cMin = today.getMinutes();
var cSec = today.getSeconds();
alert( monthNames[cMonth] + " " +cDate + "," +cYear + " " +cHour+ ":" + cMin+ ":" +cSec );
回答by daniellmb
Have you looked at this
你看过这个
dateFormat(now, "mmm dd, yyyy hh:MM:ss");
it is light weight (1.2 KB) and supports the following formats
它重量轻(1.2 KB)并支持以下格式
- d Day of the month as digits; no leading zero for single-digit days.
- dd Day of the month as digits; leading zero for single-digit days.
- ddd Day of the week as a three-letter abbreviation.
- dddd Day of the week as its full name.
- m Month as digits; no leading zero for single-digit months.
- mm Month as digits; leading zero for single-digit months.
- mmm Month as a three-letter abbreviation.
- mmmm Month as its full name.
- yy Year as last two digits; leading zero for years less than 10.
- yyyy Year represented by four digits.
- h Hours; no leading zero for single-digit hours (12-hour clock).
- hh Hours; leading zero for single-digit hours (12-hour clock).
- H Hours; no leading zero for single-digit hours (24-hour clock).
- HH Hours; leading zero for single-digit hours (24-hour clock).
- M Minutes; no leading zero for single-digit minutes. Uppercase M unlike CF timeFormat's m to avoid conflict with months.
- MM Minutes; leading zero for single-digit minutes. Uppercase MM unlike CF timeFormat's mm to avoid conflict with months.
- s Seconds; no leading zero for single-digit seconds.
- ss Seconds; leading zero for single-digit seconds.
- l or L Milliseconds. l gives 3 digits. L gives 2 digits.
- t Lowercase, single-character time marker string: a or p.
- tt Lowercase, two-character time marker string: am or pm.
- T Uppercase, single-character time marker string: A or P. Uppercase T unlike CF's t to allow for user-specified casing.
- TT Uppercase, two-character time marker string: AM or PM. Uppercase TT unlike CF's tt to allow for user-specified casing.
- Z US timezone abbreviation, e.g. EST or MDT. With non-US timezones or in the Opera browser, the GMT/UTC offset is returned, e.g. GMT-0500
- o GMT/UTC timezone offset, e.g. -0500 or +0230.
- S The date's ordinal suffix (st, nd, rd, or th). Works well with d.
- '…' or "…" Literal character sequence. Surrounding quotes are removed.
- UTC: Must be the first four characters of the mask. Converts the date from local time to UTC/GMT/Zulu time before applying the mask. The "UTC:" prefix is removed.
- d 以数字表示的月份日期;个位数天数没有前导零。
- dd 以数字表示的月份日期;个位数天数前导零。
- ddd 星期几作为三个字母的缩写。
- dddd 星期几作为其全名。
- m 月份为数字;个位数月份没有前导零。
- mm 月份为数字;个位数月份的前导零。
- mmm 月份作为三个字母的缩写。
- mmmm 月份作为其全名。
- yy 年份作为最后两位数字;小于 10 年的前导零。
- yyyy 用四位数字表示的年份。
- 小时;单位数小时(12 小时制)没有前导零。
- hh 小时;个位数小时前导零(12 小时制)。
- H 小时;单位数小时(24 小时制)没有前导零。
- 小时;个位数小时前导零(24 小时制)。
- M 分钟;一位数的分钟没有前导零。大写 M 与 CF timeFormat 的 m 不同,以避免与月份发生冲突。
- MM 分钟;个位数分钟前导零。大写 MM 与 CF timeFormat 的 mm 不同,以避免与月份发生冲突。
- 秒;个位数的秒数没有前导零。
- ss 秒;个位数秒前导零。
- l 或 L 毫秒。l 给出 3 位数字。L 给出 2 位数字。
- t 小写的单字符时间标记字符串:a 或 p。
- tt 小写、两个字符的时间标记字符串:am 或 pm。
- T 大写、单字符时间标记字符串:A 或 P。大写 T 与 CF 的 t 不同,允许用户指定大小写。
- TT 大写,两个字符的时间标记字符串:AM 或 PM。大写 TT 与 CF 的 tt 不同,允许用户指定大小写。
- Z 美国时区缩写,例如 EST 或 MDT。对于非美国时区或在 Opera 浏览器中,返回 GMT/UTC 偏移量,例如 GMT-0500
- o GMT/UTC 时区偏移,例如 -0500 或 +0230。
- S 日期的序数后缀(st、nd、rd 或 th)。与 d 配合良好。
- '...' 或 "..." 文字字符序列。周围的引号被删除。
- UTC:必须是掩码的前四个字符。在应用掩码之前将日期从本地时间转换为 UTC/GMT/Zulu 时间。“UTC:”前缀被删除。
回答by Alex KeySmith
You may be interested in http://www.datejs.com/
您可能对http://www.datejs.com/感兴趣
thanks for the comments guys, I should of really linked more directly to the formatting options.
感谢您的评论,我应该更直接地链接到格式选项。