No more nonsense, first of all the formatting methods posted to Everyone
Copy Code code as follows:
var mydate = new Date ();
Mydate.getyear (); Get Current year (2-bit)
Mydate.getfullyear (); Get full year (4-bit, 1970-????)
Mydate.getmonth (); Get Current month (0-11, 0 for January)
Mydate.getdate (); Get the current day (1-31)
Mydate.getday (); Get Current week x (0-6, 0 for Sunday)
Mydate.gettime (); Gets the current time (number of milliseconds since 1970.1.1)
Mydate.gethours (); Get the current number of hours (0-23)
Mydate.getminutes (); Get the current number of minutes (0-59)
Mydate.getseconds (); Get the current number of seconds (0-59)
Mydate.getmilliseconds (); Get current number of milliseconds (0-999)
Mydate.tolocaledatestring (); Get Current date
var mytime=mydate.tolocaletimestring (); Get current time
Mydate.tolocalestring (); Get Date and time
Can be said to be a Web project indispensable JavaScript class library, it can help you quickly solve the client programming many problems, below posted a JS format Time method.
Copy Code code as follows:
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]:
("+ o[k]"). substr (("" + o[k]). length);
return format;
}
The above code must first be declared and then used. How to use:
var d =new Date (). Format (' Yyyy-mm-dd ');
Another method:
In JavaScript, date objects are dates, so how do you export a Date object in a custom format?
You can now tell you that the Date object has four built-in methods for output in string format, respectively:
1) togmtstring, display a date in GMT format
2) tolocalestring, display a date in the local operating system format
3) tolocaledatestring, display the date part of a Date object in local format
4) tolocaletimestring, display the time part of a Date object in local format
Although the JavaScript Date object has built-in methods for the output as strings, these strings are not in our control format, so what if we need a special format that we customize?
Don't worry, Jsjava provides a dedicated class that specifically formats the string output for a date, you can download Jsjava-2.0.zip, introduce the src/jsjava/text/dateformat.js, or directly introduce jslib/ Jsjava-2.0.js, the sample code is as follows:
Copy Code code as follows:
var df=new simpledateformat ();//jsjava1.0 need to use DateFormat object, don't make a mistake.
Df.applypattern ("Yyyy-mm-dd HH:mm:ss");
var date=new date (2007,3,30,10,59,51);
var str=df.format (date);
document.write (str);//Display result: 2007-04-30 10:59:51
You can see from the above example, you need to do is to specify pattern, then the pattern of yyyy, MM and so what mean? If you have studied the date format of Java, then you should know that these are placeholders that have special functions, such as Y for years, yyyy for four digits, such as 1982, Here are some of the special characters supported in pattern and their meanings (the table below is from the official Java documentation and modified appropriately):
Copy Code code as follows:
G Era designator [Url=]text[/url] AD
Y year [Url=]year[/url] 1996; 96
M Month in the year [Url=]month[/url] July; June June; 07
W Week in year [Url=]number[/url] 27
W Week in month [Url=]number[/url] 2
D Day in year [Url=]number[/url] 189
D Day in month [Url=]number[/url] 10
F Day of week in month [Url=]number[/url] 2
E Day in week [Url=]text[/url] Tuesday; Tue
A am/pm marker [Url=]text[/url] pm
H Hour in Day (0-23) [Url=]number[/url] 0
K Hour in Day (1-24) [Url=]number[/url] 24
K Hour in AM/PM (0-11) [Url=]number[/url] 0
H Hour in AM/PM (1-12) [Url=]number[/url] 12
M Minute in hour [Url=]number[/url] 30
s Second in minute [Url=]number[/url] 55
S millisecond [Url=]number[/url] 978