Serialization of core objects (date objects)

Source: Internet
Author: User

A time object is a commonly used object. It is inseparable from operations such as time output and time judgment. Apart from the time objects in Javascript, VBScript also has many time objects, which are very useful. The following describes the process.

It is a built-in object, rather than the attributes of other objects, allowing users to execute various processes that use date and time.
Methods: Get the time method, set the time method, and convert the time method.

Obtain the time method:
Getdate (): view the date object and return the date
Getday () returns the day of the week
Gethours () return hours
Getminutes () returns the number of minutes
Getmonth () returns the month value.
Getseconds () returns the number of seconds
Gettime () returns the complete time
Getyear () returns the year
  

Time Setting method:
Setdate () changes the date of the date object
Sethours () change the hour
Setminutes () changes the number of minutes
Setmonth () Change month
Setseconds () changed in seconds
Settime () changes the complete time
Setyear () changes the year

Conversion time method:
Togmtstring () converts the date object's date (a value) into a GMT time string and returns a value similar to the following: WEDS, 15 June l997 14:02:02 GMT (the exact format depends on the operating system running on the computer)
Tolocalestring () converts the date (a value) of the date object into a string and uses the specified date format configured on the computer where it is located.
UTC () uses date UTC (year, month, day, hour, minute, second), and is optional since January 1, 1970 00:00:00) returns the date in milliseconds.
Notes:

1. Get the date and year and set the date and year. The strange problem is that you cannot set the month (compared with the strange ONE ):
<Script language = "JavaScript">
D = new date ();
Alert (D. tolocalestring ());
D. setdate (25 );
Alert (D. tolocalestring ());
D. setyear (2000 );
Alert (D. tolocalestring ());
</SCRIPT>

2. It is best to use the getfullyear () method to obtain the year.

3. Because JS starts from 0 for the month, you need to add 1 for the month operation.

The following are several classic and frequently used examples of time. I hope this will improve your performance. Thank you for continuing to follow this post...

1. Convert 2005-8-5 to 2005-08-05 format
<Script language = "JavaScript">
VaR strdate = '2017-8-5 ';
Window. Alert (strdate. Replace (/\ B (\ W) \ B/g, '0 $1 '));
</SCRIPT>

2. Get the number of days
<SCRIPT type = "text/JavaScript">
<! --
Alert ("days separated:" + (new date ('2014/1/15')-New Date ('2014/2/18')/2005/8/2003/9 + "day ")
// -->
</SCRIPT>

3. Get the interval
<SCRIPT>
VaR d1 = new date ("20:08:00 ");
VaR D2 = new date ("10:18:03 ");
VaR D3 = d1-d2;
VaR H = math. Floor (D3/3600000 );
VaR M = math. Floor (d3-h * 3600000)/60000 );
VaR S = (d3-h * 3600000-m * 60000)/1000;
Alert ("difference" + H + "Hour" + M + "Minute" + S + "second ");
</SCRIPT>

4. Get the date of today
<Script language = "JavaScript">
D = new date ();
Alert (D. getfullyear () + "year" + (D. getmonth () + 1) + "month" + D. getdate () + "day ");
</SCRIPT>

6. Convert numeric date to Chinese Character
<HTML>
<Head>
<Title> new document </title>
</Head>

<Body>

<Script language = JavaScript>
Date. Prototype. getread = function ()
{
VaR values = new array ("zero", "one", "two", "three", "four", "five", "Six", "Seven ", "8", "9 ");
VaR returnvalue, temp;
Returnvalue = This. getyear () + "year ";
Temp = (this. getmonth () + 1) + "month" + this. getdate () + "day ";
Temp = temp. replace (/(\ D)/g, "$1 10 $2 "). replace (/1 10/g, "10 "). replace (/10 0/G, "10 ");
Returnvalue + = temp;
Returnvalue = returnvalue. Replace (/\ D/g, function (STS) {return values [parseint (STS)]});
Return returnvalue;
}

VaR T = new date ();
Document. Write (T. getread ());
</SCRIPT>
</Body>
</Html>

7. Obtain the date of the previous n days or the next n days.
Method 1:
<SCRIPT type = "text/JavaScript">
Function showdate (N)
{
VaR UOM = new date ()-0 + N * 86400000 );
UOM = UOM. getfullyear () + "-" + (UOM. getmonth () + 1) + "-" + UOM. getdate ();
Return UOM;
}

Window. Alert ("today is:" + showdate (0 ));
Window. Alert ("yesterday:" + showdate (-1 ));
Window. Alert ("Tomorrow is:" + showdate (1 ));
Window. Alert ("10 days ago:" + showdate (-10 ));
Window. Alert ("5 days later:" + showdate (5 ));
</SCRIPT>
Method 2:
<SCRIPT type = "text/JavaScript">
Function showdate (N)
{
VaR UOM = new date ();
UOM. setdate (UOM. getdate () + n );
UOM = UOM. getfullyear () + "-" + (UOM. getmonth () + 1) + "-" + UOM. getdate ();
Return UOM;
}

Window. Alert ("today is:" + showdate (0 ));
Window. Alert ("yesterday:" + showdate (-1 ));
Window. Alert ("Tomorrow is:" + showdate (1 ));
Window. Alert ("10 days ago:" + showdate (-10 ));
Window. Alert ("5 days later:" + showdate (5 ));
</SCRIPT>
Method 3 (Sorry, vsscript is used in this city ):
<Script language = "VBScript">
Function showdate (N)
Showdate = dateadd ("D", date (), n)
End Function
Msgbox "today is:" & showdate (0)
Msgbox "yesterday:" & showdate (-1)
Msgbox "Tomorrow is:" & showdate (1)
Msgbox "ten days ago:" & showdate (-10)
Msgbox "five days later:" & showdate (5)
</SCRIPT>
Method 4:
<Script language = "JavaScript">
Date. Prototype. getdays = function (){
VaR _ newdate = new date ();
_ Newdate. setmonth (_ newdate. getmonth () + 1 );
_ Newdate. setdate (0 );
$ _ Days = _ newdate. getdate ();
Delete _ newdate;
Return $ _ days;
}
Function showdate (N)
{
VaR UOM = new date ();
UOM. setdate (UOM. getdate () + n );
UOM = UOM. getfullyear () + "-" + (UOM. getmonth () + 1) + "-" + UOM. getdate () + "\ n weeks" + ('day, February 5, 1234 '. charat (UOM. getday () + "\ n this month" + UOM. getdays () + "day ";
Return UOM;
}

Window. Alert ("today is:" + showdate (0 ));
Window. Alert ("yesterday:" + showdate (-1 ));
Window. Alert ("Tomorrow is:" + showdate (1 ));
Window. Alert ("10 days ago:" + showdate (-10 ));
Window. Alert ("5 days later:" + showdate (5 ));
</SCRIPT>

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.