Js format time summary

  • 2020-03-30 04:15:15
  • OfStack

Nonsense not to say, the first all kinds of formatting methods posted to everyone

var myDate = new Date(); myDate.getYear(); //Get current year (2 bits) myDate.getFullYear(); //Gets the full year (4 bits,1970-? The & # 63; The & # 63; The & # 63;) myDate.getMonth(); //Get the current month (0-11,0 for January) myDate.getDate(); //Get current day (1-31) myDate.getDay(); //Get the current week X(0-6,0 for Sunday) myDate.getTime(); //Gets the current time (number of milliseconds since 1970.1.1) myDate.getHours(); //Gets the current number of 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 the current date var mytime=myDate.toLocaleTimeString(); //Get the current time myDate.toLocaleString( ); //Gets date and time

It can be said to be an indispensable Javascript library in Web projects, which can help you quickly solve many problems of client-side programming, the following is a method of using js format time.

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 and then used. Usage:

Var d = new Date (). The format (' MM - dd yyyy - ');
Another way:

In Javascript, the Date object is Date, so how do you output a Date object in a custom format?
  We can now tell you that the Date object has four built-in methods for output in string format:
  1) toGMTString, which displays a date in GMT format
  2) toLocaleString, which displays a date in local operating system format
  3) toLocaleDateString, which displays the date part of a date object in a local format
  4) toLocaleTimeString, which displays the time part of a date object in local format
  Although Javascript's Date object has built-in methods for output as strings, we don't control the format of these strings, so what if we need our own custom special format?
  Don't try so hard, the JsJava provided in special classes, dedicated to specify the date format string output, you can download JsJava - 2.0. Zip, the introduction of the SRC/JsJava/text/DateFormat, js, or directly introducing jslib/JsJava - 2.0 js, sample code is as follows:

var df=new SimpleDateFormat();//JsJava1.0 requires the use of the DateFormat object, so make no 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 display result is: 2007-04-30 10:59:51

G Era designator [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

Related articles: