如何使用JavaScript将文本框值从'08 / 11/2012'更改为'08 -NOV-2012' 页面加载后,它应显示'08 -NOV-2012'
答案 0 :(得分:2)
我也喜欢momentjs,因为它使很多日期操作变得简单易读。 here是你的答案。
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窥视(不错的日期选择器)。
答案 1 :(得分: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();
请参阅How to change date format in JavaScript您可能需要在页面加载时将textbox.text值更改为str ..
答案 2 :(得分:0)
我可以推荐这个精彩的Javascript日期库:
答案 3 :(得分: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())
);
};