sergiu-gordienco / javascript-date-format-object
本包最新版本(1.0.3)没有提供许可证信息。
1.0.3
2015-05-07 23:14 UTC
This package is not auto-updated.
Last update: 2024-09-28 16:32:26 UTC
README
============================= Javascript-Date-Format-Object
日期和时间 - 在JavaScript中格式化日期或时间
使用示例
初始化
<script src="m_date.js" type="text/Javascript" charset="utf-8" ></script> <script type="text/javascript"> // we initialize an object that use current time ( time when object m_date was created ) var date_format = m_date(); document.write("<pre>"); document.write("\n Date: HH:mm:ss » "+m_date().format("H:i:s")); document.write("\n Date: YYYY:MM:dd » "+m_date().format("Y:m:d")); document.write("</pre>"); </script>
使用当前时间或更改它
// initialization of object var date_format = m_date(); // renew current time in object m_date().setTime(); // alert the current time alert(m_date.format("Y:m:d H:i:s")); // add a specific time **May 13 2012 13:45:20** m_date().setTime('2012 5 13 13:45:20'); // alert updated time alert(m_date.format("Y:m:d H:i:s")); // a shorter mode to write alert(m_date.setTime('2012 5 13 13:45:20').format("Y:m:d H:i:s"));
相对时间更改
// initialization of object var date_format = m_date(); // renew current time in object m_date().setTime(); // alert the current time alert(m_date.format("Y:m:d H:i:s")); // add + 60 seconds m_date().setTime(60*1000); alert(m_date.format("Y:m:d H:i:s")); // now reducing + 120 seconds m_date().setTime(-120*1000); alert(m_date.format("Y:m:d H:i:s"));
转义字符
// initialization of object var date_format = m_date(); // renew current time in object m_date().setTime(); // alert the current time but, // add a char "T" at the beginning of response alert(m_date.format("\\T Y:m:d H:i:s"));
更改月份名称
// initialization of object var date_format = m_date(); // change months names date_format.months = [ 'Ianuarie','Februarie','Martie','Aprilie','Mai','Iunie', 'Iulie','August','Septembrie','Octombrie','Noiembrie','Decembrie']; // alert the current month in short and long form alert(m_date.format("M F")); // change days names date_format.days = [ 'Duminică','Luni ','Marti','Miercuri','Joi','Vineri','Sambata']; // alert the current month in short and long form alert(m_date.format("l D"));
日期格式化的附加配置
格式
输出日期字符串的格式。请参阅以下格式化选项。还有一些预定义的日期常量可以使用,例如 DATE_RSS 包含格式字符串 'D, d M Y H:i:s'。
以下字符在格式参数字符串中是被认可的格式字符
- 日
- d - 月份中的日,带前导零的两位数字,从01到31
- D - 一个代表星期的文本表示,三个字母从Mon到Sun
- j - 月份中的日,不带前导零,从1到31
- l - (小写的 'L')一个完整的星期表示,从星期日到星期六
- N - ISO-8601星期数字表示,从1(星期一)到7(星期日)
- S - 英文序数后缀,表示月份中的日,两位数字st, nd, rd或th。与j一起使用效果很好
- w - 星期的数字表示,从0(星期日)到6(星期六)
- z - 一年中的日,从0开始(从0开始)到365
- 周 --- ---
- W - ISO-8601年中的周数,从星期一开始的一周;例如:42(一年中的第42周)
- 月份
- F - 一个完整的月份表示,如一月或三月,从一月到十二月
- m - 月份的数字表示,带前导零,从01到12
- M - 一个简短的月份表示,三个字母从Jan到Dec
- n - 月份的数字表示,不带前导零,从1到12
- t - 给定月份中的天数,从28到31
- 年
- L - 是否为闰年,如果是闰年则为1,否则为0。
- Y - 一年的完整数字表示,四位数字,例如:1999或2003
- y - 年份的两位数字表示,例如:99或03
- 时间
- a - 小写上午和下午am或pm
- A - 大写上午和下午AM或PM
- B - Swatch互联网时间,从000到999
- g - 12小时格式的小时,不带前导零,从1到12
- G - 24小时格式的小时,不带前导零,从0到23
- h - 12小时格式的小时,带前导零,从01到12
- H - 24小时格式的小时,带前导零,从00到23
- i - 带前导零的分钟,从00到59
- s - 秒,带前导零,从00到59
- u - 微秒
- 时区
- O - 格林威治时间(GMT)差异(以小时为单位)示例:+0200
- P - 格林威治时间(GMT)差异(小时和分钟之间有冒号分隔)示例:+02:00
- Z - 时区偏移量(以秒为单位)。UTC以西的时区偏移量始终为负,UTC以东的时区偏移量始终为正。-43200至50400
- 完整日期/时间
- c - ISO 8601日期;2004-02-12T15:19:21+00:00
- r - RFC 2822格式的日期示例:Thu, 21 Dec 2000 16:01:07 +0200
- U - 自Unix纪元(1970年1月1日00:00:00 GMT)以来的秒数
格式字符串中的未识别字符将按原样打印。