Js formatting time summary _ basic knowledge-js tutorial

Source: Internet
Author: User
Tags dateformat
In projects, we often encounter formatting the date and time. Next we will first summarize various time formatting methods, and then analyze them through instances, first, paste various formatting methods to everyone.

The Code is as follows:


Var myDate = new Date ();

MyDate. getYear (); // obtain the current year (2 digits)

MyDate. getFullYear (); // obtain the complete year (4 bits, 1970 -????)

MyDate. getMonth (); // obtain the current month (0-11, 0 represents January)

MyDate. getDate (); // obtain the current day (1-31)

MyDate. getDay (); // obtain X of the current week (0-6, 0 indicates Sunday)

MyDate. getTime (); // obtain the current time (milliseconds starting from 1970.1.1)

MyDate. getHours (); // obtain the current hour (0-23)

MyDate. getMinutes (); // obtain the current number of minutes (0-59)

MyDate. getSeconds (); // obtain the current number of seconds (0-59)

MyDate. getMilliseconds (); // obtain the current number of milliseconds (0-999)

MyDate. toLocaleDateString (); // obtain the current date

Var mytime = myDate. toLocaleTimeString (); // obtain the current time

MyDate. toLocaleString (); // obtain the date and time

It can be said that it is an indispensable Javascript class library in Web projects. It can help you solve many client programming problems quickly. Below is a method for formatting time with js.

The Code 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. Usage:

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

In Javascript, the Date object is Date. How do I output a Date object in a custom format?
We can tell you that the Date object has four built-in methods for output in string format:
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 Date object in Javascript provides built-in methods to output these strings, these strings are not controlled by us. Therefore, if we need to customize a special format, so what should we do?
In a rush, jsjava built a dedicated class, dedicated to the daily entry pattern of the serial output, 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:

The Code is as follows:


Var df = new SimpleDateFormat (); // The DateFormat object must be used for jsJava1.0.
Df. applyPattern ("yyyy-MM-dd HH: mm: ss ");
Var date = new Date (2007,3 );
Var str = df. format (date );
Document. write (str); // The result is: 10:59:51

Through the above example, you can see that what you need to do is to specify the pattern. What does yyyy and MM in pattern mean? If you have learned how to format dates in Java, you should know that all placeholders have special functions. For example, y indicates year and yyyy indicates year of four numbers, for example, 1982, the following lists some special characters and their meanings supported by pattern (the following table is derived from the official Java documentation and has been modified as appropriate ):

The Code is as follows:


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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.