JavaScript converts a Date to a string using Date-Mask

Source: Internet
Author: User

JavaScript converts a Date to a string using Date-Mask

This example describes how JavaScript converts a Date to a string using Date-Mask. Share it with you for your reference. The specific implementation method is as follows:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

Var MonthNames = ["January", "February", "March", "l", "May", "June", "July", "August", "September ", "October", "November", "December"];

Var DayNames = ["Sunday", "Monday", "Tueday", "Wednesday", "Thursday ",

"Friday", "Saturday"];

Var ShortMths = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug ",

"Sep", "Oct", "Nov", "Dec"];

Var Expiration days = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];

Var StringToDate = function (sDate, sFormat, cutOff ){

// Input: a date value as a string, it's format as a string e.g. 'dd-mmm-yy'

// Optional: a cutoff (integer) for 2 digit years.

// If no 'D' appears in the format string then the 1st of the month is assumed.

// If the year is 20 and the cut-off is 30 then the value will be converted

// To 2020; if the year is 40 then this will be converted to 1940.

// If no cut-off is supplied then '20' will be pre-pended to the year (YY ).

// Output: a string in the format 'yyyy/MM/dd' or''

// Will not attempt to convert certain combinations e.g. DMM, MDD, DDM, YYYYD.

Var sParsed, fndSingle;

// SParsed will be constructed in the format 'yyyy/MM/dd'

SDate = sDate. toString (). toUpperCase ();

SFormat = sFormat. toUpperCase ();

If (sFormat. search (/MMMM | MMM/) + 1) {// replace Mar/March with 03, etc.

SDate = sDate. replace (new RegExp ('+ ShortMths. join (' | ') +') [A-Z] * ', 'gi '),

Function (m ){

Var I = ShortMths. indexOf (m. charAt (0). toUpperCase () +

M. substr (1, 2). toLowerCase () + 1;

Return (I <10 )? "0" + I: "" + I). toString ();

});

SFormat = sFormat. replace (/MMMM | MMM/g, 'mm ');

}

If (sFormat. search (/DDDD | DDD/) + 1) {// replace Tue/Tuesday, etc.''

SDate = sDate. replace (new RegExp ('+ lifecycle days. join (' | ') +') [A-Z] * ', 'gi '),'');

SFormat = sFormat. replace (/DDDD | DDD/g ,'');

}

SDate = sDate. replace (/(^ | \ D) (\ d )(? = \ D | $)/g, function ($0, $1, $2 ){

// Single digits 2 with 02

Return $1 + '0' + $2;

});

SFormat = sFormat. replace (/(^ | [^ DMY]) (D | M )(? = [^ DMY] | $)/g, function ($0, $1, $2 ){

Return $1 + $2 + $2; // replace D or M with DD and MM

});

// Are there still single Ds or MS?

FndSingle = sFormat. search (/(^ | [^ D]) D ([^ D] | $) | (^ | [^ M]) M ([^ M] | $ )/) + 1;

// Do not attempt to parse, for example, 'dmm'

If (fndSingle) return '';

SFormat = sFormat. replace (/(^ | [^ Y]) (YY )(? = [^ Y] | $)/g, function ($0, $1, $2, index ){

Var tempDate = sDate. substr (0, index + 1 );

TempDate + = (cutOff )? (ParseInt (sDate. substr (index + 1, 2), 10)> cutOff )? '19': '20'): '20 ';

TempDate + = sDate. substr (index + 1 );

SDate = tempDate;

Return $1 + $2 + $2;

});

SParsed = ('yyyy/MM/dd'). replace (/YYYY | MM | DD/g, function (m ){

Return (sFormat. indexOf (m) + 1 )?

SDate. substr (sFormat. indexOf (m), m. length ):'';

});

If (sParsed. charAt (0) = '/'){

// If no year specified, assume the current year

SParsed = (new Date (). getFullYear () + sParsed;

}

If (sParsed. charAt (sParsed. length-1) = '/'){

// If no date, assume the 1st of the month

SParsed + = '01 ';

}

// Shocould end up with 10 characters ..

Return (sParsed. length = 10 )? SParsed :'';

};

I hope this article will help you design javascript programs.

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.