使用Javascript更改日期格式

Change Date format using Javascript

如何使用JavaScript将文本框值从'08 / 11/2012'更改为'08 -NOV-2012'
页面加载后,它应显示'08 -NOV-2012'


我也喜欢momentjs,因为它使很多日期操作变得简单易读。 这是你的答案。

1
var d = moment('08/11/2012',"DD/MM/YYYY").format('DD-MMM-YYYY').toUpperCase();???

只需在您的页面中包含此内容:
在加载代码之前


这是我用于日期格式化的代码

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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*jslint sub: true, todo: true, browser: true, devel: true, indent: 4, maxerr: 50, maxlen: 120 */
/*global */
/*
About: Author
George Weilenmann

About: Purpose
Adds a formatting function to the prototypal level of the Native Date object. And while it is certain that the code
could be written more concisely it ends up loosing a lot of its readability. When minified is ~1 KB in size.

About: Prerequisite

Script Requires:
Native browser support for the Date Object and access tot he prototype of the Date object

JSLint Flags:
    2012-08-11 edition

    sub - true
    todo - true
    browser - true
    devel - true

Version history:
1.2.0 - further code cleanup and renaming of variables to improve readability, changed formatString elements to be
to terminate in ;
1.1.0 - code clean up and documentation added
1.0.0 - Created prior to best practices

Licensing: +Use, +Examination, +Reverse Engineering, -Simple Distribution, +Distribution with full credit and link
*/

/*
Method: format (formatString {String})
Formats a {Date} to desired form.

Parameter: formatString {String}
Contains the desired format of the {Date}. In formatting process only recognized formatting elements are replaced
any other characters are taken at face value and returned unaltered.

Recognized Formats:
YYYY - 4 digit year
YY - 2 digit year
MMMM - Month Name
MMM - Month Abbreviation
MM - 2 digit month
M - auto digit month
DDDD - Day Name
DDD - Day Abbreviation
DD - 2 digit day
D - auto digit day
th - Ordinal suffix
hhh - 24 hour number [0-23]
hh - 2 digit hour from 12 hour clock
h - auto digit hour
mm - 2 digit minute
m - auto digit minute
ss - 2 digit second
s - auto digit second
sss - milliseconds
ampm - am/pm
AMPM - AM/PM


Returns: {String}
The processed date in string form.  If formatString is undefined or null a blank string is returned.
*/

Date.prototype.format = function (formatString) {
   "use strict";
    var
        MonthNames = [
           "January",
           "February",
           "March",
           "April",
           "May",
           "June",
           "July",
           "August",
           "September",
           "October",
           "November",
           "December"
        ],
        DayNames = [
           "Sunday",
           "Monday",
           "Tuesday",
           "Wednesday",
           "Thursday",
           "Friday",
           "Saturday"
        ],
        milliseconds = this.getMilliseconds(),
        seconds = this.getSeconds(),
        twoDigitSeconds = (seconds < 10 ? ('0' + seconds) : String(seconds)),
        minutes = this.getMinutes(),
        twoDigitMinutes = (minutes < 10 ? ('0' + minutes) : String(minutes)),
        twentyfourHours = this.getHours(),
        hours = (
            (
                twentyfourHours === 0 ?
                        24 :
                        twentyfourHours
            ) > 12 ? (twentyfourHours === 0 ? 24 : twentyfourHours) - 12 : twentyfourHours
        ),
        twoDigitHours = (hours < 10 ? ('0' + hours) : String(hours)),
        meridiem = (twentyfourHours < 12 ? 'am' : 'pm'),
        fullYear = this.getFullYear(),
        shortYear = String((fullYear)).substr(2, 2),
        monthNumber = this.getMonth() + 1,
        twoDigitMonth = (monthNumber < 10 ? ('0' + monthNumber) : monthNumber),
        MonthName = MonthNames[monthNumber - 1],
        MonthAbbreviation = MonthName.substr(0, 3),
        dayNumber = String(this.getDate()),
        twoDigitDay = (dayNumber < 10 ? ('0' + dayNumber) : String(dayNumber)),
        dayName = DayNames[this.getDay()],
        dayAbbreviation = dayName.substr(0, 3),
        ordinal = (
            /[023]1/.test(twoDigitDay) ?
                   "st" :
                    (
                        /[02]2/.test(twoDigitDay) ?
                               "nd" :
                                (
                                    /[02]3/.test(twoDigitDay) ?
                                           "rd" :
                                           "th"
                                )
                    )
        ),
        stringToFormat = String(formatString||"");

    return (
        stringToFormat
            .replace("YYYY;", fullYear)
            .replace("YY;", shortYear)
            .replace("MMMM;", MonthName)
            .replace("MMM;", MonthAbbreviation)
            .replace("MM;", twoDigitMonth)
            .replace("M;", monthNumber)
            .replace("DDDD;", dayName)
            .replace("DDD;", dayAbbreviation)
            .replace("DD;", twoDigitDay)
            .replace("D;", dayNumber)
            .replace("th;", ordinal)
            .replace("hhh;", twentyfourHours)
            .replace("hh;", twoDigitHours)
            .replace("h;", hours)
            .replace("mm;", twoDigitMinutes)
            .replace("m;", minutes)
            .replace("sss;", milliseconds)
            .replace("ss;", twoDigitSeconds)
            .replace("s;", seconds)
            .replace("ampm;", meridiem)
            .replace("AMPM;", meridiem.toUpperCase())
    );
};

我可以推荐这个精彩的Javascript日期库:

http://momentjs.com/