How to convert a string to date & date formatting method in JS how to convert a string to date & date formatting method in JS

Source: Internet
Author: User

How to convert a string into a date & date formatting method in JS

One method is date. parse (dateval), which has powerful functions, but has a fatal drawback: it does not support the commonly used "year-month-day" format, short dates can use "/" or "-" as date delimiters, but must be expressed in the format of month, day, or year, for example, "7/20/96 ".

Another method is to use split, for example:
VaR dtstr = "2006-11-25 ";
VaR dtarr = dtstr. Split ("-");
VaR dt = new date (dtarr [0], dtarr [1], dtarr [2]);

However, this method is rigid and requires a fixed date format, which can be used only when there is no way.

If we can split the year, month, and day, try to split it. For example, ASP outputs year, month, and day respectively. The new date is used for processing. The return value is of the date type.

 

 

Date formatting

  1. <Script language ="JavaScript"Type ="Text/JavaScript"> <! --
  2. /** 
  3. * Extended Date: converts date to a string in the specified format. 
  4. * Month (M), Day (D), 12 hours (H), 24 hours (H), minute (M), second (s), Week (E), quarter (q) can use 1-2 placeholders 
  5. * Year (y) can use 1-4 placeholders, Millisecond (s) can only use 1 placeholder (1-3 digits) 
  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") => 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 Tuesday 
  11. * (New date (). pattern ("yyyy-m-d h: M: S. S") => 2006-7-2. 18 
  12. */
  13. Date. Prototype. pattern =Function(FMT ){
  14. VaRO = {
  15. "M +":This. Getmonth () + 1,// Month
  16. "D +":This. Getdate (),// Day
  17. "H +":This. Gethours () % 12 = 0? 12:This. Gethours () % 12,// Hour
  18. "H +":This. Gethours (),// Hour
  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. VaRWeek = {
  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(VaRKInO ){
  40. If(NewRegexp ("("+ 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. ReturnFMT;
  45. }
  46. VaRDate =NewDate ();
  47. Window. Alert (date. pattern ("Yyyy-mm-dd hh: mm: SS"));
  48. // --> </SCRIPT>

 

 

Blogger: This sectionCodeRun the following code in the project:

VaR I = 1;
VaR dtarr = ("9-7-6"). Split ("-");
VaR statedate = new date (dtarr [0], dtarr [1], dtarr [2]);
VaR month = statedate. getmonth () + I; // get the month
Statedate. setmonth (month );
Dtsprobationerenddate. Val (statedate. Format ("yyyy-mm-dd "));

 

A problem was found to avoid the occurrence of 0 months on the date.This. Getmonth () + 1. The display time below is, Correct display should beTherefore, the following code is modified:

var dtarr = (lblprobationerstartdate. text ()). split ("-");
var statedate = new date (dtarr [0], dtarr [1], dtarr [2]);
month = statedate. getmonth () + I-1 ; // get the month
statedate. setmonth (month);
dtsprobationerenddate. val (statedate. format ("yyyy-mm-dd");

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.