var mydate = new Date ();
Mydate.getyear (); //Get Current year (2-bit)
Mydate.getfullyear (); //Get the full year (4-bit, 1970-????)
Mydate.getmonth (); //Get the current month (0-11, 0 for January)
Mydate.getdate (); //Get current day (1-31)
Mydate.getday (); //Get Current week x (0-6, 0 for Sunday)
Mydate.gettime (); //Get Current time ( Number of milliseconds since 1970.1.1)
Mydate.gethours (); //Get current hours (0-23)
Mydate.getminutes (); //Gets the current number of minutes (0-59)
Mydate.getseconds (); //Gets the current number of seconds (0-59)
Mydate.getmilliseconds (); //Gets the current number of milliseconds (0-999)
mydate.tolocaledatestring (); //Get current date
var mytime=mydate.tolocaletimestring (); //Get current time
Mydate.tolocalestring (); //Get date and time
Date Time Script Library method list
Date. Prototype.isleapyear Judging leap years
Date.prototype.Format date Formatting
DATE.PROTOTYPE.DATEADD Date Calculation
Date.prototype.DateDiff Comparison Date Difference
Date.prototype.toString Date to String
Date.prototype.toArray date split into arrays
Date.prototype.DatePart Partial information of the date taken
Date.prototype.MaxDayOfDate the maximum number of days in the month in which the date is taken
Date.prototype.WeekNumOfYear the week of the year in which the date is judged
Stringtodate String to date type
Isvaliddate Verifying date Validity
Checkdatetime Full date and time check
Daysbetween Date Days Difference
Date.prototype.format = function (format) {
var o = {
"m+": This.getmonth () +1,//month
"D+": This.getdate (),//day
"H +": this.gethours (),//hour
"m+": This.getminutes (),//minute
"S+": This.getseconds (),//second
"q+": Math.floor ((This.getmonth () +3)/3),//quarter
"S": This.getmilliseconds ()//millisecond
}
if (/(y+)/.test (format)) {
Format = Format.replace (regexp.$1, (this.getfullyear () + ""). substr (4-regexp.$1.length));
}
For (var k in O) {
if (New RegExp ("(" + K + ")"). Test (format)) {
Format = Format.replace (regexp.$1, Regexp.$1.length==1 o[k]: ("XX" + o[k]). substr (("" + o[k]). length);
}
}
return format;
}
How to use
var now = new Date ();
var nowstr = Now.format ("Yyyy-mm-dd hh:mm:ss");
Use Method 2:
var testdate = new Date ();
var teststr = Testdate.format ("yyyy mm month DD day hh hours mm min ss sec");
alert (TESTSTR);
Example:
Alert (new Date (). Format ("yyyy year mm DD Day"));
Alert (new Date (). Format ("mm/dd/yyyy"));
Alert (new Date (). Format ("YyyyMMdd"));
Alert (new Date (). Format ("Yyyy-mm-dd hh:mm:ss"));
JS format current time is YYYY-MM-DD form
function Getnowformatdate ()
{
var day = new Date ();
var year = 0;
var Month = 0;
var day = 0;
var currentdate = "";
Initialization time
Year= day.getyear ();//have Firefox next 2008 years show 108 bug
Year= day.getfullyear ();//ie Firefox can
Month= day.getmonth () +1;
Day = Day.getdate ();
Hour = Day.gethours ();
Minute = Day.getminutes ();
Second = Day.getseconds ();
CurrentDate + = year + "-";
if (Month >= 10)
{
currentdate + = Month + "-";
}
Else
{
currentdate + = "0" + Month + "-";
}
if (Day >= 10)
{
currentdate + = day;
}
Else
{
currentdate + = "0" + day;
}
return currentdate;
}
A summary of JS formatted time-date function