Home >Backend Development >PHP Tutorial >Date formatting in javascript

Date formatting in javascript

巴扎黑
巴扎黑Original
2016-11-29 11:50:13973browse

We all know that in Java and PHP languages, there are classes and functions specifically used to format date objects, such as DateFormat in Java, etc. Through these classes and functions, we can easily format a date object according to the format requirements The output is a string. For example, for the same date, December 25, 2006, the required display format may be as follows:

December 25, 2006, 2006-12-25, 12-25-2006, etc.

In Javascript, the date object is Date, so how to output a date object in a customized format?

I can tell you now that the Date object has four built-in methods for outputting to string format, which are:

1) toGMTString, displays a date in GMT format
2) toLocaleString, displays a date in local format Operating system format display
3) toLocaleDateString, displays the date part of a date object in local format
4) toLocaleTimeString, displays the time part of a date object in local format

Although Javascript's Date object provides these outputs as strings built-in method, but we do not control the format of these strings, so what should we do if we need our own customized special format?

Don’t worry, JsJava provides a dedicated class to output strings in a specified format for dates. You can download JsJava-1.0.zip and introduce src/jsjava/text/DateFormat.js in it, or directly introduce jslib /jsjava-1.0.js, the sample code is as follows:

var df=new DateFormat();
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 displayed result is: 2007-04-30 10:59:51

Through the above As you can see from the example, all you need to do is specify the pattern. So what do yyyy, MM, etc. in pattern mean? If you have studied date formatting in Java, then you should know that they are all placeholders. These placeholders have special meanings. For example, y represents the year, and yyyy represents the four-digit year, such as 1982. Listed below Some special characters supported in patterns and their meanings (the table below is quoted from Java’s official documentation, with appropriate modifications):



Letter Date or Time Component Presentation Examples
G Era designator Text AD
y Year Year 1996; 96
M Month in year Month July; Jul; 07
w Week in year Number 27
W Week in month Number 2
D Day in year Number 189
d Day in month Number 10
F Day of week in month Number 2
E Day in week Text Tuesday; Tue
a Am/pm marker Text PM
H Hour in day (0-23) Number 0
k Hour in day (1-24) Number 24
K Hour in am/pm (0-11 ) Number 0
h Hour in am/pm (1-12) Number 12
m Minute in hour Number 30
s Second in minute Number 55
S Millisecond Number 978

JsJava is very powerful and can be said to be the most popular in web projects An indispensable Javascript class library, it can help you quickly solve many problems in client-side programming. You can go to http://www.jsjava.com to download the source code and user manual.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn