JavaScript通過Date-Mask將日期轉換成字串的方法

來源:互聯網
上載者:User

JavaScript通過Date-Mask將日期轉換成字串的方法

   本文執行個體講述了JavaScript通過Date-Mask將日期轉換成字串的方法。分享給大家供大家參考。具體實現方法如下:

  ?

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", "April", "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 ShortDays = ["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. with ''

sDate = sDate.replace(new RegExp('(' + ShortDays.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';

}

// should end up with 10 characters..

return ( sParsed.length == 10 ) ? sParsed : '';

};

  希望本文所述對大家的javascript程式設計有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.