Without further ado, I will post various formatting methods to everyone
The code copy is as follows:
var myDate = new Date();
myDate.getYear(); //Get the current year (2 digits)
myDate.getFullYear(); //Get the full year (4 digits, 1970-????)
myDate.getMonth(); //Get the current month (0-11, 0 represents January)
myDate.getDate(); //Get the current day (1-31)
myDate.getDay(); //Get the current week X (0-6, 0 represents Sunday)
myDate.getTime(); //Get the current time (number of milliseconds starting from 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 the current number of milliseconds (0-999)
myDate.toLocaleDateString(); //Get the current date
var mytime=myDate.toLocaleTimeString(); //Get the current time
myDate.toLocaleString( ); //Get date and time
It can be said to be an indispensable Javascript class library in web projects. It can help you quickly solve many problems in client programming. Here is a method to format time using js.
The code copy is 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] :
("00"+ o[k]).substr((""+ o[k]).length));
return format;
}
The above code must be declared first and then used. How to use:
var d =new Date().format('yyyy-MM-dd');
Another way:
In Javascript, the date object is Date, so how do you output a date object in a customized format?
You can tell you now that the Date object has four built-in methods, which are used to output in string formats, namely:
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 Javascript's Date object provides built-in methods for these outputs as strings, these strings are not what we control the format, so what if we need a special format that we customize ourselves?
Don't worry, JsJava provides a dedicated class that specializes in string output for dates in a specified format. You can download JsJava-2.0.zip, introduce src/jsjava/text/DateFormat.js, or directly introduce jslib/jsjava-2.0.js. The sample code is as follows:
The code copy is as follows:
var df=new SimpleDateFormat();//jsJava1.0 requires using 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);//The result is: 2007-04-30 10:59:51
From the above example, you can see that all you need to do is specify the pattern. So what does yyyy, MM, etc. mean in the pattern? If you have learned Java date formatting, then you should know that they are all placeholders. These placeholders have special functions, such as y represents year, and yyy represents the year of four numbers, such as 1982. Here are some special characters supported in pattern and their meanings (the following table is quoted from the official Java documentation and has been appropriately modified):
The code copy is as follows:
G Era designer [url=]Text[/url] AD
y Year [url=]Year[/url] 1996; 96
M Month in year [url=]Month[/url] July; Jul; 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