In VBScript, you can easily convert a string-type date into a date of the date type through cdate.
In JS, it is not that easy.
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
-
- <Script language ="JavaScript"Type ="Text/JavaScript"> <! --
-
- /**
- * Extended Date: converts date to a string in the specified format.
-
- * Month (M), Day (D), 12 hours (H), 24 hours (H), minute (M), second (s), Week (E), quarter (q) can use 1-2 placeholders
-
- * Year (y) can use 1-4 placeholders, Millisecond (s) can only use 1 placeholder (1-3 digits)
-
- * Eg:
-
- * (New date (). pattern ("yyyy-mm-dd hh: mm: Ss. s") => 08:09:04. 423
- * (New date (). pattern ("yyyy-mm-dd e hh: mm: SS") => 20:09:04
-
- * (New date (). pattern ("yyyy-mm-dd EE hh: mm: SS") => 08:09:04 Tuesday
-
- * (New date (). pattern ("yyyy-mm-dd EEE hh: mm: SS") => 08:09:04 Tuesday
-
- * (New date (). pattern ("yyyy-m-d h: M: S. S") => 2006-7-2. 18
-
- */
- Date. Prototype. pattern =Function(FMT ){
-
- VaRO = {
-
- "M +":This. Getmonth () + 1,// Month
-
- "D +":This. Getdate (),// Day
- "H +":This. Gethours () % 12 = 0? 12:This. Gethours () % 12,// Hour
-
- "H +":This. Gethours (),// Hour
-
- "M +":This. Getminutes (),// Minute
- "S +":This. Getseconds (),// Second
-
- "Q +": Math. Floor ((This. Getmonth () + 3)/3 ),// Quarter
-
- "S":This. Getmilliseconds ()// Millisecond
-
- };
- VaRWeek = {
-
- "0":"\ U65e5",
-
- "1":"\ U4e00",
-
- "2":"\ U4e8c",
-
- "3":"\ U4e09",
- "4":"\ U56db",
-
- "5":"\ U4e94",
-
- "6":"\ U516d"
-
- };
-
- If(/(Y +)/. Test (FMT )){
- FMt = FMT. Replace (Regexp. $1 ,(This. Getfullyear () +""). Substr (4-Regexp. $1. Length ));
-
- }
-
- If(/(E +)/. Test (FMT )){
- FMt = FMT. Replace (Regexp. $1, (Regexp. $1. length> 1 )? (Regexp. $1. length> 2?"\ U661f \ u671f":"\ U5468"):"") + Week [This. Getday () +""]);
-
- }
-
- For(VaRKInO ){
- If(NewRegexp ("("+ K +")"). Test (FMT )){
-
- FMt = FMT. Replace (Regexp. $1, (Regexp. $1. Length = 1 )? (O [k]): ("00"+ O [k]). substr ((""+ O [k]). Length )));
-
- }
-
- }
- ReturnFMT;
-
- }
-
-
- VaRDate =NewDate ();
-
- Window. Alert (date. pattern ("Yyyy-mm-dd hh: mm: SS"));
-
- // --> </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");