简体   繁体   English

在javascript中将日期转换为特定格式?

[英]Convert date to specific format in javascript?

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????? 有什么建议?????

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 );

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; d作为数字的月份日; no leading zero for single-digit days. 单位数天没有前导零。
  • dd Day of the month as digits; dd每月的日期为数字; leading zero for single-digit days. 单位数天数为零。
  • ddd Day of the week as a three-letter abbreviation. ddd星期几作为三个字母的缩写。
  • dddd Day of the week as its full name. dddd星期几作为其全名。
  • m Month as digits; m月份为数字; no leading zero for single-digit months. 单个数字月份没有前导零。
  • mm Month as digits; mm月份为数字; leading zero for single-digit months. 单位数月份领先零。
  • mmm Month as a three-letter abbreviation. mmm月份为三个字母的缩写。
  • mmmm Month as its full name. mmmm月份的全名。
  • yy Year as last two digits; yy作为最后两位数的年份; leading zero for years less than 10. 多年来不到10年领先零。
  • yyyy Year represented by four digits. yyyy年份由四位数代表。
  • h Hours; h小时; no leading zero for single-digit hours (12-hour clock). 单位数小时(12小时制)没有前导零。
  • hh Hours; ......小时; leading zero for single-digit hours (12-hour clock). 单位数小时(12小时制)的前导零。
  • H Hours; H小时; no leading zero for single-digit hours (24-hour clock). 单位数小时(24小时制)没有前导零。
  • HH Hours; HH小时; leading zero for single-digit hours (24-hour clock). 单位数小时(24小时制)的前导零。
  • M Minutes; M分钟; no leading zero for single-digit minutes. 单位数分钟没有前导零。 Uppercase M unlike CF timeFormat's m to avoid conflict with months. 大写字母M与CF timeFormat不同,以避免与月份冲突。
  • MM Minutes; MM分钟; leading zero for single-digit minutes. 单位数分钟的前导零。 Uppercase MM unlike CF timeFormat's mm to avoid conflict with months. 大写MM不像CF timeFormat的mm,以避免与月份冲突。
  • s Seconds; s秒; no leading zero for single-digit seconds. 单位数秒没有前导零。
  • ss Seconds; ss秒; leading zero for single-digit seconds. 单位数秒的前导零。
  • l or L Milliseconds. l或L毫秒。 l gives 3 digits. 我给出3位数。 L gives 2 digits. L给出2位数。
  • t Lowercase, single-character time marker string: a or p. t小写,单字符时间标记字符串:a或p。
  • tt Lowercase, two-character time marker string: am or pm. tt小写,双字符时间标记字符串:am或pm。
  • T Uppercase, single-character time marker string: A or P. Uppercase T unlike CF's t to allow for user-specified casing. T大写,单字符时间标记字符串:A或P.大写字母T不同于CF的t,允许用户指定的外壳。
  • TT Uppercase, two-character time marker string: AM or PM. TT大写,双字符时间标记字符串:AM或PM。 Uppercase TT unlike CF's tt to allow for user-specified casing. 大写TT与CF的tt不同,允许用户指定的外壳。
  • Z US timezone abbreviation, eg EST or MDT. Z US时区缩写,例如EST或MDT。 With non-US timezones or in the Opera browser, the GMT/UTC offset is returned, eg GMT-0500 对于非美国时区或在Opera浏览器中,返回GMT / UTC偏移量,例如GMT-0500
  • o GMT/UTC timezone offset, eg -0500 or +0230. o GMT / UTC时区偏移,例如-0500或+0230。
  • S The date's ordinal suffix (st, nd, rd, or th). S日期的序数后缀(st,nd,rd或th)。 Works well with d. 适用于d。
  • '…' or "…" Literal character sequence. '...'或“......”文字字符序列。 Surrounding quotes are removed. 周围的报价被删除。
  • UTC: Must be the first four characters of the mask. UTC:必须是掩码的前四个字符。 Converts the date from local time to UTC/GMT/Zulu time before applying the mask. 在应用蒙版之前,将日期从当地时间转换为UTC / GMT / Zulu时间。 The "UTC:" prefix is removed. “UTC:”前缀已删除。

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. 感谢评论家伙,我应该更直接地链接到格式化选项。

http://code.google.com/p/datejs/wiki/FormatSpecifiers http://code.google.com/p/datejs/wiki/FormatSpecifiers

这个小型库复制了JavaScript中其他语言提供的strftime()功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM