In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces how to use javascript to set the time format, the article is very detailed, has a certain reference value, interested friends must read it!
The method of setting time format with javascript: 1, use "new Date (). Format ('yyyy-MM-dd');" to format the time; 2, output the date in string format through the built-in method of js.
This article operating environment: windows7 system, javascript1.8.5 version, Dell G3 computer.
How to set the time format with javascript?
Format time with JavaScript (js)
It can be said that it is an indispensable Javascript class library in the Web project, it can help you quickly solve many problems of client-side programming, here is a way to format time with js.
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 before it is used. How to use it:
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 custom format?
I can tell you now that the Date object has four built-in methods for exporting to string format, which are:
1) toGMTString, which displays a date in GMT format
2) toLocaleString, which displays a date in the local operating system format
3) toLocaleDateString, which displays the date portion of a date object in local format
4) toLocaleTimeString, which displays the time portion of a date object in local format
Although these methods of output as strings are built into Javascript's Date object, we don't control the format of these strings, so what if we need our own customized special format?
Don't worry, JsJava provides a special class for string output of dates in a specified format. You can download JsJava-2.0.zip, introduce the src/jsjava/text/DateFormat.js in it, or directly import jslib/jsjava-2.0.js. The sample code is as follows:
Var df=new SimpleDateFormat (); / / jsJava1.0 needs to use a DateFormat object, so make no mistake: df.applyPattern ("yyyy-MM-dd HH:mm:ss"); var date=new Date (2007 date); [xss_clean] (str); / / display the result as follows: 2007-04-30 10:59:51
From the above example, you can see that all you need to do is specify pattern, so what does yyyy, MM, and so on mean in pattern? If you have studied date formatting in Java, you should know that these placeholders are placeholders with special functions, such as y for year and yyyy for four-digit year, such as 1982. Here are some special characters supported in pattern and their meanings (the following table is quoted from the official document of Java with appropriate modifications):
G Era designator [url=] text [/ url] ADy Year [url=] Year [/ url] 1996; 96m Month in year [url=] Month [/ url] July; Jul; 07w Week in year [url=] Number[ / url] 27W Week in month [url=] Number[ / url] 2D Day in year [url=] Number[ / url] 189d Day in month [url=] Number[ / url] 10F Day of week in month [url=] Number[ / url] 2e Day in week [url=] Text[ / url Tuesday] Tuea Am/pm marker [url=] text [/ url] PMH Hour in day (0-23) [url=] Number[ / url] 0k Hour in day (1-24) [url=] Number[ / url] 24K Hour in am/pm (0-11) [url=] Number[ / url] 0h Hour in am/pm (1-12) [url=] Number[ / url] 12m Minute in hour [url=] Number[ / url] 30s Second in minute [url=] Number[ / url] 55s Millisecond [url=] Number [ / url] 978 are all the contents of the article "how to use javascript to format time" Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.