Application of JavaScript format strings _javascript Tips

Source: Internet
Author: User
Tags datetime
After some design, finally completed this function. After the introduction of this JS, you can configure the format string to output various custom date formats.

Flow chart

As you can see, a format string is actually a string containing a specific character and then replaced with the specified value based on its actual meaning.
In this article, only the date object is used as an example, but the value of the format string is not just that. In what scenario can you use a format string? I hope you can find the answer at the end of this article.
Algorithm Introduction
I'll use an example below to illustrate the algorithm for the format string. This example formats the day section of the date, for example, in 2008-8-8, if the format string is "D", Output "8", or "08" if the format string is "DD", or "five" if the format string is "dddd", and if the format string is "dddd", the output " Friday ". Where parameter d is a Date object, format is a string:
Copy Code code as follows:

//Format Day
function formatday (d, format) {
while format.indexof ("D") &G T -1) {
var regex =/[d]+/;
format = Format.replace (Regex,function (w) {
switch (w.length) {
Case 0:break;
Case 1:
return d.getdate ();
Case 2:
return D.getdate () < 10? "0" + d.getdate (): D.getdate ();
Case 3:
Switch (D.getday ()) {
Case 0:
return "Day";
Case 1:
return "one";
Case 2:
return "two";
Case 3:
return "three";
Case 4:
return "four";
Case 5:
return "V";
Case 6:
return "VI";
}
Default:
Switch (D.getday ()) {
Case 0:
return "Sunday";
Case 1:
return "Monday";
Case 2:
return "Tuesday";
Case 3:
return "Wednesday";
Case 4:
return "Thursday";
Case 5:
return "Friday";
Case 6:
return "Saturday";
}
}
});
}
return format;
}

As you can see, the core part of this is:
Copy Code code as follows:

while (Format.indexof ("D") >-1) {
var regex =/[d]+/;
format = format.replace (regex, Function (W) {
Switch (w.length) {
Case 0:break;
Case 1:
Todo
Case 2:
Todo
Case 3:
Todo
Case x:
Todo
Default
Todo
}
});
}

Explain:
1. Use a while loop to execute as long as the format string contains a specific character;
2. Declare a regular Expression object/[x]+/, where x represents a particular character;
3. Replace a specific character with the Replace method of a String object;
4. Perform different operations depending on the length of the specific string that is matched (in this example, "D", "DD", "ddd" represents different meanings).
Format string Description
Taking 508-1-9 14:3:5 as an example

format string

Describe

Example

Y

The format of the year. The year is made up of century + years.

"Y" Output 8

"yy" Output 08

"YYY" Output 508

"YYYY" Output 0508

"Yyyyyy" Output 000508

M

Format the month.

"M" Output 1

"MM" Output 01

"MMM" or more output month

D

Format Day.

"D" Output 9

"DD" Output 09

"DDD" Output One

"dddd" or more output Monday

H,h

Format the hour. H represents a 24-hour system and H represents a 12-hour system.

"H" Output 14

"HH" or more output 14

"H" Output 2

"HH" or more output 02

M

Format minutes.

"M" Output 3

"MM" or more output 03

S

Format seconds

"S" Output 5

"SS" or more output 05


More settings, you can do it by yourself.

Example
After referencing this JS, the test results in the browser's console are as follows:

How, there is no feeling of heartbeat ...

By the way, each browser's console exhale:

Browser

Shortcut keys

Chrome

Ctrl + Shift + J

IE8

F12

FireFox

Forgot. The console in Firefox is not native, it's a plugin called Firebug.


Source
The following code can be downloaded in dateextension.js
Copy Code code as follows:

Date.prototype.ToString = function (format) {
if (typeof (format) = = "string") {
Return FormatDateTime (this, format);
}
Return FormatDateTime (This, "Yyyy-mm-dd HH:mm:ss");
}
Format a DateTime object
function FormatDateTime (d, format) {
Format = formatyear (d, format);
Format = formatmonth (d, format);
Format = Formatday (d, format);
Format = Formathour (d, format);
Format = Formatminute (d, format);
Format = Formatsecond (d, format);
return format;
}
Format of the Year
function Formatyear (d, format) {
var fullyear = D.getfullyear (); The full year
var century = Math.floor (fullyear/100); Century
var year = fullyear% 100; Age
while (Format.indexof ("Y") >-1) {
var regex =/[y]+/;
Format = Format.replace (Regex,function (w) {
If the format string is "Y" or "yy", only the age is returned. Otherwise return century + years
Switch (w.length) {
Case 0:break;
Case 1:
return to year;
Case 2:
Return year < 10? "0" + year:year;
Default
var Yearpart = Year < 10? "0" + year:year;
var centurypart = "";
for (var i = 0; i < w.length-2-century.tostring (). length; i++) {
Centurypart + = "0";
}
Centurypart + = century;
return Centurypart + Yearpart;
}
});
}
return format;
}
Format month
function Formatmonth (d, format) {
var month = D.getmonth () + 1;
while (Format.indexof ("M") >-1) {
var regex =/[m]+/;
Format = Format.replace (Regex,function (w) {
Switch (w.length) {
Case 0:break;
Case 1:
Return month;
Case 2:
Return Month < 10? "0" + month:month;
Default
Switch (month) {
Case 1:
return "January";
Case 2:
return "February";
Case 3:
return "March";
Case 4:
return "April";
Case 5:
return "May";
Case 6:
return "June";
Case 7:
return "July";
Case 8:
return "August";
Case 9:
return "September";
Case 10:
return "October";
Case 11:
return "November";
Case 12:
return "December";
}
}
});
}
return format;
}
Format Day
function Formatday (d, format) {
while (Format.indexof ("D") >-1) {
var regex =/[d]+/;
Format = Format.replace (Regex,function (w) {
Switch (w.length) {
Case 0:break;
Case 1:
return D.getdate ();
Case 2:
Return D.getdate () < 10? "0" + d.getdate (): D.getdate ();
Case 3:
Switch (D.getday ()) {
Case 0:
Return "Day";
Case 1:
return "one";
Case 2:
return "two";
Case 3:
return "three";
Case 4:
return "four";
Case 5:
return "five";
Case 6:
Return "Six";
}
Default
Switch (D.getday ()) {
Case 0:
return "Sunday";
Case 1:
return "Monday";
Case 2:
return "Tuesday";
Case 3:
return "Wednesday";
Case 4:
return "Thursday";
Case 5:
return "Friday";
Case 6:
return "Saturday";
}
}
});
}
return format;
}
Format hours
H: 24-hour system
H:12-hour system
function Formathour (d, format) {
while (Format.indexof ("H") >-1) {
var regex =/[h]+/;
Format = Format.replace (Regex,function (w) {
Switch (w.length) {
Case 0:break;
Case 1:
return d.gethours ();
Default
Return D.gethours () < 10? "0" + d.gethours (): D.gethours ();
}
});
}
while (Format.indexof ("H") >-1) {
var regex =/[h]+/;
Format = Format.replace (Regex,function (w) {
Switch (w.length) {
Case 0:break;
Case 1:
return d.gethours () > 12? D.gethours ()-12:d.gethours ();
Default
var t = d.gethours () > 12? D.gethours ()-12:d.gethours ();
Return T < 10? "0" + t:t;
}
});
}
return format;
}
Format minutes
function Formatminute (d, format) {
while (Format.indexof ("M") >-1) {
var regex =/[m]+/;
Format = Format.replace (Regex,function (w) {
Switch (w.length) {
Case 0:break;
Case 1:
return D.getminutes ();
Default
Return D.getminutes () < 10? "0" + d.getminutes (): D.getminutes ();
}
});
}
return format;
}
Format seconds
function Formatsecond (d, format) {
while (Format.indexof ("s") >-1) {
var regex =/[s]+/;
Format = Format.replace (Regex,function (w) {
Switch (w.length) {
Case 0:break;
Case 1:
return D.getseconds ();
Default
Return D.getseconds () < 10? "0" + d.getseconds (): D.getseconds ();
}
});
}
return format;
}

The resources used in this article
Dateextension.js Download
School view of the Consortium
More support browsing for date objects
JS implementation of date Operation class DateTime function code

Download the PDF version of the address
Related Article

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.