0

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

4

4 回答 4

2

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

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

只需将其包含在您的页面中: <script src="http://cdnjs.cloudflare.com/ajax/libs/moment.js/1.7.2/moment.min.js"></script>在您的代码加载之前。

我可能还建议使用<input type="date" />带有date-input polyfill的 HTML5 窥视(漂亮的日期选择器) 。

于 2012-11-15T23:52:26.677 回答
1

你可以自己做

var mydate = new Date(form.startDate.value);
var month = ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"][mydate.getMonth()];
var str = mydate.getdate() + '-' + month + '-' + mydate.getFullYear();

请参阅如何在 JavaScript 中更改日期格式您可能必须在页面加载时将 textbox.text 值更改为 str 。

于 2012-11-15T14:54:21.723 回答
0

我可以推荐这个出色的 Javascript 日期库:

http://momentjs.com/

于 2012-11-15T14:49:02.737 回答
0

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

/*jslint sub: true, todo: true, browser: true, devel: true, indent: 4, maxerr: 50, maxlen: 120 */
/*global */
/*
About: Author
George Weilenmann <email at abyssoft@gmail.com>

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())
    );
};
于 2012-11-16T00:42:17.887 回答