写道
var dt = Date.fromString($("#txtDate").val());
var str = dt.asString("ddd dd/mmm/yy");
var str = dt.asString("ddd dd/mmm/yy");
javascript 如何对日期进行格式化?
以“ddd dd/mmm/yy” 格式输出 "12/Jan/2010"。
date.js 写道
/*
* Date prototype extensions. Doesn't depend on any
* other code. Doens't overwrite existing methods.
*
* Adds dayNames, abbrDayNames, monthNames and abbrMonthNames static properties and isLeapYear,
* isWeekend, isWeekDay, getDaysInMonth, getDayName, getMonthName, getDayOfYear, getWeekOfYear,
* setDayOfYear, addYears, addMonths, addDays, addHours, addMinutes, addSeconds methods
*
* Copyright (c) 2006 Jörn Zaefferer and Brandon Aaron (brandon.aaron@gmail.com || http://brandonaaron.net)
*
* Additional methods and properties added by Kelvin Luck: firstDayOfWeek, dateFormat, zeroTime, asString, fromString -
* I've added my name to these methods so you know who to blame if they are broken!
*
* Dual licensed under the MIT and GPL licenses:
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl.html
*
*/
/**
* An Array of day names starting with Sunday.
*
* @example dayNames[0]
* @result 'Sunday'
*
* @name dayNames
* @type Array
* @cat Plugins/Methods/Date
*/
Date.dayNames = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
/**
* An Array of abbreviated day names starting with Sun.
*
* @example abbrDayNames[0]
* @result 'Sun'
*
* @name abbrDayNames
* @type Array
* @cat Plugins/Methods/Date
*/
Date.abbrDayNames = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
/**
* An Array of month names starting with Janurary.
*
* @example monthNames[0]
* @result 'January'
*
* @name monthNames
* @type Array
* @cat Plugins/Methods/Date
*/
Date.monthNames = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
/**
* An Array of abbreviated month names starting with Jan.
*
* @example abbrMonthNames[0]
* @result 'Jan'
*
* @name monthNames
* @type Array
* @cat Plugins/Methods/Date
*/
Date.abbrMonthNames = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
/**
* The first day of the week for this locale.
*
* @name firstDayOfWeek
* @type Number
* @cat Plugins/Methods/Date
* @author Kelvin Luck
*/
Date.firstDayOfWeek = 1;
/**
* The format that string dates should be represented as (e.g. 'dd/mm/yyyy' for UK, 'mm/dd/yyyy' for US, 'yyyy-mm-dd' for Unicode etc).
*
* @name format
* @type String
* @cat Plugins/Methods/Date
* @author Kelvin Luck
*/
Date.format = 'dd/mmm/yy';
//Date.format = 'mm/dd/yyyy';
//Date.format = 'yyyy-mm-dd';
//Date.format = 'dd mmm yy';
/**
* The first two numbers in the century to be used when decoding a two digit year. Since a two digit year is ambiguous (and date.setYear
* only works with numbers < 99 and so doesn't allow you to set years after 2000) we need to use this to disambiguate the two digit year codes.
*
* @name format
* @type String
* @cat Plugins/Methods/Date
* @author Kelvin Luck
*/
Date.fullYearStart = '20';
(function() {
/**
* Adds a given method under the given name
* to the Date prototype if it doesn't
* currently exist.
*
* @private
*/
function add(name, method) {
if( !Date.prototype[name] ) {
Date.prototype[name] = method;
}
};
/**
* Checks if the year is a leap year.
*
* @example var dtm = new Date("01/12/2008");
* dtm.isLeapYear();
* @result true
*
* @name isLeapYear
* @type Boolean
* @cat Plugins/Methods/Date
*/
add("isLeapYear", function() {
var y = this.getFullYear();
return (y%4==0 && y%100!=0) || y%400==0;
});
/**
* Checks if the day is a weekend day (Sat or Sun).
*
* @example var dtm = new Date("01/12/2008");
* dtm.isWeekend();
* @result false
*
* @name isWeekend
* @type Boolean
* @cat Plugins/Methods/Date
*/
add("isWeekend", function() {
return this.getDay()==0 || this.getDay()==6;
});
/**
* Check if the day is a day of the week (Mon-Fri)
*
* @example var dtm = new Date("01/12/2008");
* dtm.isWeekDay();
* @result false
*
* @name isWeekDay
* @type Boolean
* @cat Plugins/Methods/Date
*/
add("isWeekDay", function() {
return !this.isWeekend();
});
/**
* Gets the number of days in the month.
*
* @example var dtm = new Date("01/12/2008");
* dtm.getDaysInMonth();
* @result 31
*
* @name getDaysInMonth
* @type Number
* @cat Plugins/Methods/Date
*/
add("getDaysInMonth", function() {
return [31,(this.isLeapYear() ? 29:28),31,30,31,30,31,31,30,31,30,31][this.getMonth()];
});
/**
* Gets the name of the day.
*
* @example var dtm = new Date("01/12/2008");
* dtm.getDayName();
* @result 'Saturday'
*
* @example var dtm = new Date("01/12/2008");
* dtm.getDayName(true);
* @result 'Sat'
*
* @param abbreviated Boolean When set to true the name will be abbreviated.
* @name getDayName
* @type String
* @cat Plugins/Methods/Date
*/
add("getDayName", function(abbreviated) {
return abbreviated ? Date.abbrDayNames[this.getDay()] : Date.dayNames[this.getDay()];
});
/**
* Gets the name of the month.
*
* @example var dtm = new Date("01/12/2008");
* dtm.getMonthName();
* @result 'Janurary'
*
* @example var dtm = new Date("01/12/2008");
* dtm.getMonthName(true);
* @result 'Jan'
*
* @param abbreviated Boolean When set to true the name will be abbreviated.
* @name getDayName
* @type String
* @cat Plugins/Methods/Date
*/
add("getMonthName", function(abbreviated) {
return abbreviated ? Date.abbrMonthNames[this.getMonth()] : Date.monthNames[this.getMonth()];
});
/**
* Get the number of the day of the year.
*
* @example var dtm = new Date("01/12/2008");
* dtm.getDayOfYear();
* @result 11
*
* @name getDayOfYear
* @type Number
* @cat Plugins/Methods/Date
*/
add("getDayOfYear", function() {
var tmpdtm = new Date("1/1/" + this.getFullYear());
return Math.floor((this.getTime() - tmpdtm.getTime()) / 86400000);
});
/**
* Get the number of the week of the year.
*
* @example var dtm = new Date("01/12/2008");
* dtm.getWeekOfYear();
* @result 2
*
* @name getWeekOfYear
* @type Number
* @cat Plugins/Methods/Date
*/
add("getWeekOfYear", function() {
return Math.ceil(this.getDayOfYear() / 7);
});
/**
* Set the day of the year.
*
* @example var dtm = new Date("01/12/2008");
* dtm.setDayOfYear(1);
* dtm.toString();
* @result 'Tue Jan 01 2008 00:00:00'
*
* @name setDayOfYear
* @type Date
* @cat Plugins/Methods/Date
*/
add("setDayOfYear", function(day) {
this.setMonth(0);
this.setDate(day);
return this;
});
/**
* Add a number of years to the date object.
*
* @example var dtm = new Date("01/12/2008");
* dtm.addYears(1);
* dtm.toString();
* @result 'Mon Jan 12 2009 00:00:00'
*
* @name addYears
* @type Date
* @cat Plugins/Methods/Date
*/
add("addYears", function(num) {
this.setFullYear(this.getFullYear() + num);
return this;
});
/**
* Add a number of months to the date object.
*
* @example var dtm = new Date("01/12/2008");
* dtm.addMonths(1);
* dtm.toString();
* @result 'Tue Feb 12 2008 00:00:00'
*
* @name addMonths
* @type Date
* @cat Plugins/Methods/Date
*/
add("addMonths", function(num) {
var tmpdtm = this.getDate();
this.setMonth(this.getMonth() + num);
if (tmpdtm > this.getDate())
this.addDays(-this.getDate());
return this;
});
/**
* Add a number of days to the date object.
*
* @example var dtm = new Date("01/12/2008");
* dtm.addDays(1);
* dtm.toString();
* @result 'Sun Jan 13 2008 00:00:00'
*
* @name addDays
* @type Date
* @cat Plugins/Methods/Date
*/
add("addDays", function(num) {
//this.setDate(this.getDate() + num);
this.setTime(this.getTime() + (num*86400000) );
return this;
});
/**
* Add a number of hours to the date object.
*
* @example var dtm = new Date("01/12/2008");
* dtm.addHours(24);
* dtm.toString();
* @result 'Sun Jan 13 2008 00:00:00'
*
* @name addHours
* @type Date
* @cat Plugins/Methods/Date
*/
add("addHours", function(num) {
this.setHours(this.getHours() + num);
return this;
});
/**
* Add a number of minutes to the date object.
*
* @example var dtm = new Date("01/12/2008");
* dtm.addMinutes(60);
* dtm.toString();
* @result 'Sat Jan 12 2008 01:00:00'
*
* @name addMinutes
* @type Date
* @cat Plugins/Methods/Date
*/
add("addMinutes", function(num) {
this.setMinutes(this.getMinutes() + num);
return this;
});
/**
* Add a number of seconds to the date object.
*
* @example var dtm = new Date("01/12/2008");
* dtm.addSeconds(60);
* dtm.toString();
* @result 'Sat Jan 12 2008 00:01:00'
*
* @name addSeconds
* @type Date
* @cat Plugins/Methods/Date
*/
add("addSeconds", function(num) {
this.setSeconds(this.getSeconds() + num);
return this;
});
/**
* Sets the time component of this Date to zero for cleaner, easier comparison of dates where time is not relevant.
*
* @example var dtm = new Date();
* dtm.zeroTime();
* dtm.toString();
* @result 'Sat Jan 12 2008 00:01:00'
*
* @name zeroTime
* @type Date
* @cat Plugins/Methods/Date
* @author Kelvin Luck
*/
add("zeroTime", function() {
this.setMilliseconds(0);
this.setSeconds(0);
this.setMinutes(0);
this.setHours(0);
return this;
});
/**
* Returns a string representation of the date object according to Date.format.
* (Date.toString may be used in other places so I purposefully didn't overwrite it)
*
* @example var dtm = new Date("01/12/2008");
* dtm.asString();
* @result '12/01/2008' // (where Date.format == 'dd/mm/yyyy'
*
* @name asString
* @type Date
* @cat Plugins/Methods/Date
* @author Kelvin Luck
*/
add("asString", function(format) {
var r = format || Date.format;
return r
.split('yyyy').join(this.getFullYear())
.split('yy').join((this.getFullYear() + '').substring(2))
.split('mmmm').join(this.getMonthName(false))
.split('mmm').join(this.getMonthName(true))
.split('mm').join(_zeroPad(this.getMonth()+1))
// modified by Link Yang
.split('ddd').join(this.getDayName(true))
.split('dd').join(_zeroPad(this.getDate()));
});
/**
* Returns a new date object created from the passed String according to Date.format or false if the attempt to do this results in an invalid date object
* (We can't simple use Date.parse as it's not aware of locale and I chose not to overwrite it incase it's functionality is being relied on elsewhere)
*
* @example var dtm = Date.fromString("12/01/2008");
* dtm.toString();
* @result 'Sat Jan 12 2008 00:00:00' // (where Date.format == 'dd/mm/yyyy'
*
* @name fromString
* @type Date
* @cat Plugins/Methods/Date
* @author Kelvin Luck
*/
Date.fromString = function(s)
{
var f = Date.format;
var d = new Date('01/01/1977');
var mLength = 0;
var iM = f.indexOf('mmmm');
if (iM > -1) {
for (var i=0; i<Date.monthNames.length; i++) {
var mStr = s.substr(iM, Date.monthNames[i].length);
if (Date.monthNames[i] == mStr) {
mLength = Date.monthNames[i].length - 4;
break;
}
}
d.setMonth(i);
} else {
iM = f.indexOf('mmm');
if (iM > -1) {
var mStr = s.substr(iM, 3);
for (var i=0; i<Date.abbrMonthNames.length; i++) {
if (Date.abbrMonthNames[i] == mStr) break;
}
d.setMonth(i);
} else {
d.setMonth(Number(s.substr(f.indexOf('mm'), 2)) - 1);
}
}
var iY = f.indexOf('yyyy');
if (iY > -1) {
if (iM < iY)
{
iY += mLength;
}
d.setFullYear(Number(s.substr(iY, 4)));
} else {
if (iM < iY)
{
iY += mLength;
}
// TODO - this doesn't work very well - are there any rules for what is meant by a two digit year?
d.setFullYear(Number(Date.fullYearStart + s.substr(f.indexOf('yy'), 2)));
}
var iD = f.indexOf('dd');
if (iM < iD)
{
iD += mLength;
}
d.setDate(Number(s.substr(iD, 2)));
if (isNaN(d.getTime())) {
return false;
}
return d;
};
// utility method
var _zeroPad = function(num) {
var s = '0'+num;
return s.substring(s.length-2)
//return ('0'+num).substring(-2); // doesn't work on IE :(
};
})();
* Date prototype extensions. Doesn't depend on any
* other code. Doens't overwrite existing methods.
*
* Adds dayNames, abbrDayNames, monthNames and abbrMonthNames static properties and isLeapYear,
* isWeekend, isWeekDay, getDaysInMonth, getDayName, getMonthName, getDayOfYear, getWeekOfYear,
* setDayOfYear, addYears, addMonths, addDays, addHours, addMinutes, addSeconds methods
*
* Copyright (c) 2006 Jörn Zaefferer and Brandon Aaron (brandon.aaron@gmail.com || http://brandonaaron.net)
*
* Additional methods and properties added by Kelvin Luck: firstDayOfWeek, dateFormat, zeroTime, asString, fromString -
* I've added my name to these methods so you know who to blame if they are broken!
*
* Dual licensed under the MIT and GPL licenses:
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl.html
*
*/
/**
* An Array of day names starting with Sunday.
*
* @example dayNames[0]
* @result 'Sunday'
*
* @name dayNames
* @type Array
* @cat Plugins/Methods/Date
*/
Date.dayNames = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
/**
* An Array of abbreviated day names starting with Sun.
*
* @example abbrDayNames[0]
* @result 'Sun'
*
* @name abbrDayNames
* @type Array
* @cat Plugins/Methods/Date
*/
Date.abbrDayNames = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
/**
* An Array of month names starting with Janurary.
*
* @example monthNames[0]
* @result 'January'
*
* @name monthNames
* @type Array
* @cat Plugins/Methods/Date
*/
Date.monthNames = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
/**
* An Array of abbreviated month names starting with Jan.
*
* @example abbrMonthNames[0]
* @result 'Jan'
*
* @name monthNames
* @type Array
* @cat Plugins/Methods/Date
*/
Date.abbrMonthNames = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
/**
* The first day of the week for this locale.
*
* @name firstDayOfWeek
* @type Number
* @cat Plugins/Methods/Date
* @author Kelvin Luck
*/
Date.firstDayOfWeek = 1;
/**
* The format that string dates should be represented as (e.g. 'dd/mm/yyyy' for UK, 'mm/dd/yyyy' for US, 'yyyy-mm-dd' for Unicode etc).
*
* @name format
* @type String
* @cat Plugins/Methods/Date
* @author Kelvin Luck
*/
Date.format = 'dd/mmm/yy';
//Date.format = 'mm/dd/yyyy';
//Date.format = 'yyyy-mm-dd';
//Date.format = 'dd mmm yy';
/**
* The first two numbers in the century to be used when decoding a two digit year. Since a two digit year is ambiguous (and date.setYear
* only works with numbers < 99 and so doesn't allow you to set years after 2000) we need to use this to disambiguate the two digit year codes.
*
* @name format
* @type String
* @cat Plugins/Methods/Date
* @author Kelvin Luck
*/
Date.fullYearStart = '20';
(function() {
/**
* Adds a given method under the given name
* to the Date prototype if it doesn't
* currently exist.
*
* @private
*/
function add(name, method) {
if( !Date.prototype[name] ) {
Date.prototype[name] = method;
}
};
/**
* Checks if the year is a leap year.
*
* @example var dtm = new Date("01/12/2008");
* dtm.isLeapYear();
* @result true
*
* @name isLeapYear
* @type Boolean
* @cat Plugins/Methods/Date
*/
add("isLeapYear", function() {
var y = this.getFullYear();
return (y%4==0 && y%100!=0) || y%400==0;
});
/**
* Checks if the day is a weekend day (Sat or Sun).
*
* @example var dtm = new Date("01/12/2008");
* dtm.isWeekend();
* @result false
*
* @name isWeekend
* @type Boolean
* @cat Plugins/Methods/Date
*/
add("isWeekend", function() {
return this.getDay()==0 || this.getDay()==6;
});
/**
* Check if the day is a day of the week (Mon-Fri)
*
* @example var dtm = new Date("01/12/2008");
* dtm.isWeekDay();
* @result false
*
* @name isWeekDay
* @type Boolean
* @cat Plugins/Methods/Date
*/
add("isWeekDay", function() {
return !this.isWeekend();
});
/**
* Gets the number of days in the month.
*
* @example var dtm = new Date("01/12/2008");
* dtm.getDaysInMonth();
* @result 31
*
* @name getDaysInMonth
* @type Number
* @cat Plugins/Methods/Date
*/
add("getDaysInMonth", function() {
return [31,(this.isLeapYear() ? 29:28),31,30,31,30,31,31,30,31,30,31][this.getMonth()];
});
/**
* Gets the name of the day.
*
* @example var dtm = new Date("01/12/2008");
* dtm.getDayName();
* @result 'Saturday'
*
* @example var dtm = new Date("01/12/2008");
* dtm.getDayName(true);
* @result 'Sat'
*
* @param abbreviated Boolean When set to true the name will be abbreviated.
* @name getDayName
* @type String
* @cat Plugins/Methods/Date
*/
add("getDayName", function(abbreviated) {
return abbreviated ? Date.abbrDayNames[this.getDay()] : Date.dayNames[this.getDay()];
});
/**
* Gets the name of the month.
*
* @example var dtm = new Date("01/12/2008");
* dtm.getMonthName();
* @result 'Janurary'
*
* @example var dtm = new Date("01/12/2008");
* dtm.getMonthName(true);
* @result 'Jan'
*
* @param abbreviated Boolean When set to true the name will be abbreviated.
* @name getDayName
* @type String
* @cat Plugins/Methods/Date
*/
add("getMonthName", function(abbreviated) {
return abbreviated ? Date.abbrMonthNames[this.getMonth()] : Date.monthNames[this.getMonth()];
});
/**
* Get the number of the day of the year.
*
* @example var dtm = new Date("01/12/2008");
* dtm.getDayOfYear();
* @result 11
*
* @name getDayOfYear
* @type Number
* @cat Plugins/Methods/Date
*/
add("getDayOfYear", function() {
var tmpdtm = new Date("1/1/" + this.getFullYear());
return Math.floor((this.getTime() - tmpdtm.getTime()) / 86400000);
});
/**
* Get the number of the week of the year.
*
* @example var dtm = new Date("01/12/2008");
* dtm.getWeekOfYear();
* @result 2
*
* @name getWeekOfYear
* @type Number
* @cat Plugins/Methods/Date
*/
add("getWeekOfYear", function() {
return Math.ceil(this.getDayOfYear() / 7);
});
/**
* Set the day of the year.
*
* @example var dtm = new Date("01/12/2008");
* dtm.setDayOfYear(1);
* dtm.toString();
* @result 'Tue Jan 01 2008 00:00:00'
*
* @name setDayOfYear
* @type Date
* @cat Plugins/Methods/Date
*/
add("setDayOfYear", function(day) {
this.setMonth(0);
this.setDate(day);
return this;
});
/**
* Add a number of years to the date object.
*
* @example var dtm = new Date("01/12/2008");
* dtm.addYears(1);
* dtm.toString();
* @result 'Mon Jan 12 2009 00:00:00'
*
* @name addYears
* @type Date
* @cat Plugins/Methods/Date
*/
add("addYears", function(num) {
this.setFullYear(this.getFullYear() + num);
return this;
});
/**
* Add a number of months to the date object.
*
* @example var dtm = new Date("01/12/2008");
* dtm.addMonths(1);
* dtm.toString();
* @result 'Tue Feb 12 2008 00:00:00'
*
* @name addMonths
* @type Date
* @cat Plugins/Methods/Date
*/
add("addMonths", function(num) {
var tmpdtm = this.getDate();
this.setMonth(this.getMonth() + num);
if (tmpdtm > this.getDate())
this.addDays(-this.getDate());
return this;
});
/**
* Add a number of days to the date object.
*
* @example var dtm = new Date("01/12/2008");
* dtm.addDays(1);
* dtm.toString();
* @result 'Sun Jan 13 2008 00:00:00'
*
* @name addDays
* @type Date
* @cat Plugins/Methods/Date
*/
add("addDays", function(num) {
//this.setDate(this.getDate() + num);
this.setTime(this.getTime() + (num*86400000) );
return this;
});
/**
* Add a number of hours to the date object.
*
* @example var dtm = new Date("01/12/2008");
* dtm.addHours(24);
* dtm.toString();
* @result 'Sun Jan 13 2008 00:00:00'
*
* @name addHours
* @type Date
* @cat Plugins/Methods/Date
*/
add("addHours", function(num) {
this.setHours(this.getHours() + num);
return this;
});
/**
* Add a number of minutes to the date object.
*
* @example var dtm = new Date("01/12/2008");
* dtm.addMinutes(60);
* dtm.toString();
* @result 'Sat Jan 12 2008 01:00:00'
*
* @name addMinutes
* @type Date
* @cat Plugins/Methods/Date
*/
add("addMinutes", function(num) {
this.setMinutes(this.getMinutes() + num);
return this;
});
/**
* Add a number of seconds to the date object.
*
* @example var dtm = new Date("01/12/2008");
* dtm.addSeconds(60);
* dtm.toString();
* @result 'Sat Jan 12 2008 00:01:00'
*
* @name addSeconds
* @type Date
* @cat Plugins/Methods/Date
*/
add("addSeconds", function(num) {
this.setSeconds(this.getSeconds() + num);
return this;
});
/**
* Sets the time component of this Date to zero for cleaner, easier comparison of dates where time is not relevant.
*
* @example var dtm = new Date();
* dtm.zeroTime();
* dtm.toString();
* @result 'Sat Jan 12 2008 00:01:00'
*
* @name zeroTime
* @type Date
* @cat Plugins/Methods/Date
* @author Kelvin Luck
*/
add("zeroTime", function() {
this.setMilliseconds(0);
this.setSeconds(0);
this.setMinutes(0);
this.setHours(0);
return this;
});
/**
* Returns a string representation of the date object according to Date.format.
* (Date.toString may be used in other places so I purposefully didn't overwrite it)
*
* @example var dtm = new Date("01/12/2008");
* dtm.asString();
* @result '12/01/2008' // (where Date.format == 'dd/mm/yyyy'
*
* @name asString
* @type Date
* @cat Plugins/Methods/Date
* @author Kelvin Luck
*/
add("asString", function(format) {
var r = format || Date.format;
return r
.split('yyyy').join(this.getFullYear())
.split('yy').join((this.getFullYear() + '').substring(2))
.split('mmmm').join(this.getMonthName(false))
.split('mmm').join(this.getMonthName(true))
.split('mm').join(_zeroPad(this.getMonth()+1))
// modified by Link Yang
.split('ddd').join(this.getDayName(true))
.split('dd').join(_zeroPad(this.getDate()));
});
/**
* Returns a new date object created from the passed String according to Date.format or false if the attempt to do this results in an invalid date object
* (We can't simple use Date.parse as it's not aware of locale and I chose not to overwrite it incase it's functionality is being relied on elsewhere)
*
* @example var dtm = Date.fromString("12/01/2008");
* dtm.toString();
* @result 'Sat Jan 12 2008 00:00:00' // (where Date.format == 'dd/mm/yyyy'
*
* @name fromString
* @type Date
* @cat Plugins/Methods/Date
* @author Kelvin Luck
*/
Date.fromString = function(s)
{
var f = Date.format;
var d = new Date('01/01/1977');
var mLength = 0;
var iM = f.indexOf('mmmm');
if (iM > -1) {
for (var i=0; i<Date.monthNames.length; i++) {
var mStr = s.substr(iM, Date.monthNames[i].length);
if (Date.monthNames[i] == mStr) {
mLength = Date.monthNames[i].length - 4;
break;
}
}
d.setMonth(i);
} else {
iM = f.indexOf('mmm');
if (iM > -1) {
var mStr = s.substr(iM, 3);
for (var i=0; i<Date.abbrMonthNames.length; i++) {
if (Date.abbrMonthNames[i] == mStr) break;
}
d.setMonth(i);
} else {
d.setMonth(Number(s.substr(f.indexOf('mm'), 2)) - 1);
}
}
var iY = f.indexOf('yyyy');
if (iY > -1) {
if (iM < iY)
{
iY += mLength;
}
d.setFullYear(Number(s.substr(iY, 4)));
} else {
if (iM < iY)
{
iY += mLength;
}
// TODO - this doesn't work very well - are there any rules for what is meant by a two digit year?
d.setFullYear(Number(Date.fullYearStart + s.substr(f.indexOf('yy'), 2)));
}
var iD = f.indexOf('dd');
if (iM < iD)
{
iD += mLength;
}
d.setDate(Number(s.substr(iD, 2)));
if (isNaN(d.getTime())) {
return false;
}
return d;
};
// utility method
var _zeroPad = function(num) {
var s = '0'+num;
return s.substring(s.length-2)
//return ('0'+num).substring(-2); // doesn't work on IE :(
};
})();
发表评论
-
jquery mobile demo
2011-03-11 16:07 2407a simple message pad using jque ... -
use maxlength and required of jquery
2010-04-26 10:23 768I found that maxlength and requ ... -
给图片加入onclick方法
2010-04-09 18:12 1491写道 <img src="../image ... -
juery unbind方法
2010-04-09 18:10 949jquery 写道 $(e).find(" ... -
showdialog模态窗口中的POST方法
2010-04-09 18:07 2636showdialog 在IE8中的post方法是打开一个新窗口 ... -
js 分割字符串
2010-04-07 18:13 4540后台返回result 写道 <cfset res ...
相关推荐
js日期时间格式化js日期时间格式化js日期时间格式化js日期时间格式化js日期时间格式化js日期时间格式化js日期时间格式化js日期时间格式化
这篇博客文章“js 时间格式化”可能探讨了如何使用JavaScript来优雅地展示和处理日期与时间。 在JavaScript中,`Date`对象是处理日期和时间的基础。创建一个`Date`对象可以获取当前时间,例如: ```javascript var ...
js通用日期格式化工具,直接引用该工具类就可以使用,方便快捷,建议大家收藏此工具类,该工具很好的解决了前端页面显示日期格式问题
在JavaScript中,我们可以使用内置的`Date`对象来创建、操作和格式化日期。 首先,`Date`对象是JavaScript中的内置构造函数,可以用来创建一个新的日期实例。例如: ```javascript let date = new Date(); // 创建...
#### 三、原生JavaScript日期格式化方法 **1. 使用`Date`对象的内置方法** JavaScript的`Date`对象提供了一些基本的方法来获取日期的不同部分,如`getFullYear()`、`getMonth()`等。 - **示例代码**: ```...
通过不同的格式字符串,可以轻松地获取到所需的日期时间格式。此外,第二种方法还提供了更丰富的格式选项,如星期的显示,适用于需要更复杂日期格式化需求的应用场景。 ### 使用建议 虽然上述两种方法非常实用,但...
// 对Date的扩展,将 Date 转化为指定格式的String // 月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符, // 年(y)可以用 1-4 个占位符,毫秒(S)只能用 1 个占位符(是 1-3 位的数字) //...
JS时间格式化,JS两个时间差,JS当前日期的昨天明天 ,JS当前日期周几
- 常见的日期时间格式化方法可以将Date对象转换为指定格式的字符串。 - `Date.prototype.isLeapYear` 方法判断是否为闰年。 - `Date.prototype.Format` 方法自定义格式化日期时间。 - 格式化中常用的是:YYYY...
JS 日期格式化和加减,适用于将日期格式转化为yyyy-MM-DD HH:mm:ss格式,以及日期的加减
JQuery 的一个日期格式化插件,支持"MM-dd HH:mm"等格式的设定;并支持日期时间转义,即距当前过去了多久,如"3分钟前","1天前","2周前".
此外,除了手动格式化,还可以使用一些库,如Moment.js或date-fns,它们提供了更丰富的日期时间处理功能和易用的API,可以轻松实现各种复杂的日期时间格式化需求。不过,对于基础的格式化需求,了解和掌握JavaScript...
在Web应用中,日期时间格式化的灵活性对于提高用户体验至关重要,因此掌握这一技能非常实用。 #### 描述:对前端日期进行格式化 这段描述提供了关于日期格式化功能的具体用法示例,展示了如何使用自定义格式字符串...
js日期格式化,对Date的扩展,将 Date 转化为指定格式的String。月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符, 年(y)可以用 1-4 个占位符,毫秒(S)只能用 1 个占位符(是 1-3 位的数字)
在这个例子中,当用户点击按钮时,`formatDate`函数会被调用,将当前日期时间格式化后显示在页面的`<p>`元素中。 `doc.txt`文件可能包含了关于如何使用这些代码的说明,包括如何运行示例,以及`Control.js`中可能...
这篇博文链接提供了一个实用的示例,讲解了如何在JavaScript中处理时间格式化。 首先,我们了解JavaScript中的Date对象。Date对象是JavaScript内置的对象类型,用于处理日期和时间。通过new Date()可以创建一个表示...
JavaScript时间格式化插件是一种工具,它允许开发者以自定义的格式显示日期和时间,使得在Web应用中处理和展示时间信息更为灵活和方便。在JavaScript中,原生的Date对象虽然提供了基本的时间处理功能,但其输出格式...
### JavaScript中的Trim函数和日期时间格式化函数 在JavaScript中,字符串处理和日期操作是非常常见的需求,尤其是在数据清洗、格式转换等场景下。本文将详细介绍如何利用自定义方法实现字符串的Trim功能以及日期...
在探讨JavaScript时间格式化函数之前,了解一些关于JavaScript中的时间对象是很有必要的。JavaScript中的Date对象用于处理日期和时间。Date对象支持多种方法,例如获取年、月、日、小时、分钟和秒等。不同的浏览器和...