Javascript Note: date object and Operation Method

Source: Internet
Author: User
// Constructor new date (milliseconds) // new date (year, month, day, hours, minutes, seconds, MS) // PS: month parameter range: 0-11 // key point: the date object is converted to a string in the specified format, and the string is converted to a date // eg: new date (datestring) // new date ("yyyy-mm-dd hh: mm: SS") // new date ("yyyy/mm/dd hh: mm: SS ") it can also be mm/DD/YYYY // new date ("YYYY mm dd hh: mm: SS"). The order of year, month, and day cannot be changed. Chrome Firefox is normal. // new date ("YYYY, mm, dd hh: mm: SS ") the order of year, month, and day cannot be changed. Chrome Firefox is normal. // new date (" Month day, year hh: mm: SS ") the month can be abbreviated in English, comma can be replaced by space, and the order of year, month, and day can be changed
1 // <! [CDATA [2/** 3 * For date extension, convert date to string 4 * month (M), Day (D), 12 hours (h) in the specified format), 24 hours (H), minute (M), second (s), Week (E), quarter (q) can use 1-2 placeholders 5 * Year (y) you can use 1-4 placeholders. in milliseconds (s), you can only use 1 placeholder (a number ranging from 1 to 3) 6 * eg: 7 * (new date ()). pattern ("yyyy-mm-dd hh: mm: Ss. s ") => 08:09:04. 423 8 * (new date ()). pattern ("yyyy-mm-dd e hh: mm: SS") ==>2009-03-10 20:09:04 9 * (new date ()). pattern ("yyyy-mm-dd EE hh: mm: SS") ==> 08:09:04 Tuesday 10 * (new date ()). pattern ("yyyy-mm-dd EEE hh: mm: SS") ==> 08:09:04 11 * (new date ()). pattern ("yyyy-m-d h: M: S. s ") ==>. 18 12 */13 Date. prototype. format = function (FMT) {14 var o = {15 "m +": This. getmonth () + 1, // month 16 "d +": This. getdate (), // day 17 "H +": This. gethours () % 12 = 0? 12: This. gethours () % 12, // 12 hours 18 "H +": This. gethours (), // 24 hours 19 "m +": This. getminutes (), // minute 20 "s +": This. getseconds (), // second 21 "q +": math. floor (this. getmonth () + 3)/3), // quarter 22 "S": This. getmilliseconds () // millisecond 23}; 24 var week = {25 "0": "\ u65e5", 26 "1": "\ u4e00", 27 "2 ": "\ u4e8c", 28 "3": "\ u4e09", 29 "4": "\ u56db", 30 "5": "\ u4e94", 31 "6 ": "\ u516d" 32}; 33 If (/(Y + )/. test (FMT) {34 FMt = FMT. replace (Regexp. $1, (this. getfullyear () + ""). substr (4-Regexp. $1. length); 35} 36 IF (/(E + )/. test (FMT) {37 FMt = FMT. replace (Regexp. $1, (Regexp. $1. length> 1 )? (Regexp. $1. length> 2? "\ U661f \ u671f": "\ u5468"): "") + week [this. getday () + ""]); 38} 39 for (var k in O) {40 if (New Regexp ("(" + K + ")"). test (FMT) {41 FMt = FMT. replace (Regexp. $1, (Regexp. $1. length = 1 )? (O [k]): ("00" + O [k]). substr ("" + O [k]). length); 42} 43} 44 return FMT; 45} 46 //]>

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.