Usage of day dates in JavaScript

Source: Internet
Author: User
Tags current time dateformat getdate string format

var date = new Date ();
Date.getyear (); Get Current year (2-bit)
Date.getfullyear (); Get full year (4-bit, 1970-????)
Date.getmonth (); Get Current month (0-11, 0 for January)
Date.getdate (); Get the current day (1-31)
Date.getday (); Get Current week x (0-6, 0 for Sunday)
Date.gettime (); Gets the current time (number of milliseconds since 1970.1.1)
Date.gethours (); Get the current number of hours (0-23)
Date.getminutes (); Get the current number of minutes (0-59)
Date.getseconds (); Get the current number of seconds (0-59)
Date.getmilliseconds (); Get current number of milliseconds (0-999)
Date.tolocaledatestring (); Get Current date
var time=date.tolocaletimestring (); Get current time
Date.tolocalestring (); Get Date and time

<script language= "Web Effects" type= "Text/javascript" >
function ShowTime () {
document.getElementById ("Time"). InnerHTML = new Date (). tolocaletimestring (); Get current system time
SetTimeout ("ShowTime ()", 1000);/Set timed refresh one second update
}
function Datedemo () {
var d, day = ""; declaring variables
D = new Date (); Create Date Object Www.111cn.net
var week = new Array ("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");
Day + + d.getfullyear () + "year"; Get year
Day + + (D.getmonth () + 1) + "month"; Get month
Day + + d.getdate () + "th"; Get Day
Day + + week[d.getday ()];
document.getElementById (' time '). title = day;
}
ShowTime (); Datedemo ();
</script>

<span id= "Time" ></span>

Related functions

function Getmainhead () {

$ ("Labelwelcome"). InnerHTML = Getwelcomebytime () + "Today is:" + getsimpledate () + "" + getweekday () + "" + getc Ndate () + solarterm (new Date ());

}

function Getwelcomebytime () {

var now = new Date ();

var hour = now.gethours ();

var hello = "";

if (Hour < 4) {

Hello = "late at night";

}

else if (Hour < 7) {

Hello = "Good Morning";

}

else if (Hour < 9) {

Hello = "Good Morning";

}

else if (Hour < 12) {

Hello = "Good Morning";

}

else if (Hour < 14) {

Hello = "Good noon";

}

else if (Hour < 17) {

Hello = "Good Afternoon";

}

else if (Hour < 19) {

Hello = "Hello";

}

else if (Hour < 22) {

Hello = "Good evening";

}

else {

Hello = "late at night";

}

return hello;

}

function Getsimpledate () {

var today = new Date ();

var date = (Today.getyear () < Today.getyear () + 1900:today.getyear ()) + "year" + (Today.getmonth () + 1) + "month" + to Day.getdate () + "Day";

return date;

}

function Getweekday () {

var today = new Date ();

var weeks = new Array ("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");

Return Weeks[today.getday ()];

}

Get Lunar Calendar date

function Getcndate () {

var today = new Date ();

Return Cndateofdatestr (today);

}

function Cndateofdatestr (DATEGL) {

if (cnmonthofdate (DATEGL) = "0") return "Please adjust your computer date!";

else return "Lunar calendar:" + cnyearofdate (DATEGL) + "" + cnmonthofdate (DATEGL) + cndayofdate (DATEGL);

}

function Solarterm (DATEGL) {

var solartermstr = new Array (

"Xiaohan", "Dahan", "Spring", "Rain", "insects", "vernal Equinox",

"Qingming", "Rain", "Summer", "Xiaoman", "grain", "Summer Solstice",

"Slight heat", "Great Heat", "beginning of Autumn", "Chushu", "Lu", "Autumnal Equinox",

"Dew", "Frost", "Winter", "Snow", "Snow", "Winter Solstice");

var differenceinmonth = new Array (

1272060, 1275495, 1281180, 1289445, 1299225, 1310355,

1321560, 1333035, 1342770, 1350855, 1356420, 1359045,

1358580, 1355055, 1348695, 1340040, 1329630, 1318455,

1306935, 1297380, 1286865, 1277730, 1274550, 1271556);

var differenceinyear = 31556926;

var begintime = new Date (1901/1/1);

Begintime.settime (947120460000);

for (; Dategl.getyear () < Begintime.getyear (); ) {

Begintime.settime (Begintime.gettime ()-differenceinyear * 1000);

}

for (; Dategl.getyear () > Begintime.getyear (); ) {

Begintime.settime (Begintime.gettime () + differenceinyear * 1000);

}

for (var M = 0; Dategl.getmonth () > Begintime.getmonth (); m++) {

Begintime.settime (Begintime.gettime () + differenceinmonth[m] * 1000);

}

if (Dategl.getdate () > Begintime.getdate ()) {

Begintime.settime (Begintime.gettime () + differenceinmonth[m] * 1000);

m++;

}

if (Dategl.getdate () > Begintime.getdate ()) {

Begintime.settime (Begintime.gettime () + differenceinmonth[m] * 1000);

M = = 23? M = 0:m++;

}

var JQ;

if (dategl.getdate () = = Begintime.getdate ()) {

JQ = "Today is <font color= ' #FF9999 ' ><b>" + solartermstr[m] + "</b></font>";

}

else {

JQ = ""

}

return JQ;

}

JavaScript no refresh gets the current time in the project need no refresh update time, and the Tootip Show Month Day week, online collection of many methods, summed up, in preparation for the next need.

View Sourceprint?<script language= "JavaScript" type= "Text/javascript" >

function ShowTime () {

document.getElementById ("Time"). InnerHTML = new Date (). tolocaletimestring (); Get current system time

SetTimeout ("ShowTime ()", 1000);/Set timed refresh one second update

}

function Datedemo () {

var d, day = ""; declaring variables

D = new Date (); Creating a Date Object

var week = new Array ("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");

Day + + d.getfullyear () + "year"; Get year

Day + + (D.getmonth () + 1) + "month"; Get month

Day + + d.getdate () + "th"; Get Day

Day + + week[d.getday ()];

document.getElementById (' time '). title = day;

}

ShowTime (); Datedemo ();

</script>

<span id= "Time" ></span>
View Sourceprint? This shows the current time in <span>, the mouse over, showing the current month and the day of the week. Similar to the time under Windows System.

JS to get the current time date: We can also display their own combination of format

View Sourceprint?var date = new Date ();

Date.getyear (); Get Current year (2-bit)

Date.getfullyear (); Get full year (4-bit, 1970-????)

Date.getmonth (); Get Current month (0-11, 0 for January)

Date.getdate (); Get the current day (1-31)

Date.getday (); Get Current week x (0-6, 0 for Sunday)

Date.gettime (); Gets the current time (number of milliseconds since 1970.1.1)

Date.gethours (); Get the current number of hours (0-23)

Date.getminutes (); Get the current number of minutes (0-59)

Date.getseconds (); Get the current number of seconds (0-59)

Date.getmilliseconds (); Get current number of milliseconds (0-999)

Date.tolocaledatestring (); Get Current date

var time=date.tolocaletimestring (); Get current time

Date.tolocalestring (); Get Date and 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]:
("+ o[k]"). substr (("" + o[k]). length);
return format;
}

The above code must first be declared and then used. How to use:
  
   
   
var d = new Date (). Format (' Yyyy-mm-dd ');

Another method:
In JavaScript, date objects are dates, so how do you export a Date object in a custom format?
You can now tell you that the Date object has four built-in methods for output in string format, respectively:
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 JavaScript Date object has built-in methods for the output as strings, these strings are not in our control format, so what if we need a special format that we customize?
Don't worry, Jsjava provides a dedicated class that specifically formats the string output for a date, 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:
  
   
   
var df=new simpledateformat ();//jsjava1.0 need to use DateFormat object, don't make a 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);//Display result: 2007-04-30 10:59:51
You can see from the above example, you need to do is to specify pattern, then the pattern of yyyy, MM and so what mean? If you have studied the date format of Java, then you should know that these are placeholders that have special functions, such as Y for years, yyyy for four digits, such as 1982, Here are some of the special characters supported in pattern and their meanings (the table below is from the official Java documentation and modified appropriately):
 
   
   
G Era designator [Url=]text[/url] AD
y year [Url=]year[/url] 1996;
M Month in the year [Url=]month[/url] July; June June; Modified
W Week in year [Url=]number[/url]
W Week in month [Url=]number[/url] 2
D day in Ye AR [Url=]number[/url] 189
D Day in month [Url=]number[/url] a
F day of week in month [Url=]numbe R[/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]-
K Hour in am/pm (0-11) [Url=]number[/url] 0
H Hour in am/pm (1 -12) [Url=]number[/url]
M Minute in hour [Url=]number[/url] a
s Second in Minute [url=]number [/URL]
S millisecond [Url=]number[/url] 978
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.