Javascript: basic usage of Date object

Source: Internet
Author: User

Javascript: basic usage of Date object

This article code from w3school: http://www.w3school.com.cn

1. Get the current date:

document.write("Current time: "+new Date());
2. Obtain the timestamp in milliseconds ):

document.write(new Date().getTime());
3. Set year, month, and day (required for the Year, and optional for the month and day ):

var d = new Date();  d.setFullYear(2012,3,16)  document.write(d);  document.write("
"); document.write(d.getTime());
4. Return the current day of the week:

document.write(new Date().getDay())

5. Comparison of date objects: 

Function compareDate () {var oneday = new Date (); oneday. setFullYear (, 26); var currentday = new Date (); if (oneday
 
  
:
  



Sample Code: <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + PC9wPgo8cHJlIGNsYXNzPQ = "brush: java;"> Javascript date object (1) return the current date <script> document. write ("Current time:" + new Date (); </script> (2) return timestamp (millisecond) <script> document. write (new Date (). getTime (); </script> (3) set year, month, and day <script> var d = new Date (); d. setFullYear (2011,3, 16) document. write (d); document. write ("
"); Document. write (d. getTime (); </script> (4) convert the Date of the current day (based on UTC) to the string <script> var d2 = new Date () document. write (d2.toUTCString () </script> (5) returns the current day of the week <script> var d3 = new Date () var weekday = new Array (7) weekday [0] = "Sunday" weekday [1] = "Monday" weekday [2] = "Tuesday" weekday [3] = "weekday" weekday [4] = "Thursday" weekday [5] = "Friday" weekday [6] = "Saturday" document. write (weekday [d3.getDay ()]) </script> (6) display a time clock

<Script> function startTime () {var today = new Date (); var y = today. getFullYear (); var M = today. getMonth () + 1; var day = today. getDay (); var h = today. getHours (); var m = today. getMinutes (); var s = today. getSeconds (); m = checkTime (m); s = checkTime (s ); var content = (y + "/" + M + "/" + day + "" + h + ":" + m + ":" + s ). fontsize (6 ). fontcolor ("red"); document. getElementById ("time_text "). innerHTML = content; setTimeout ("startTime ()", 500);} function checkTime (I) {if (I <10) {return "0" + I ;} return I ;}</script> (7) Comparison of date objects

2015.3.26 what is the relationship with the current time?

Comparison<Script> function compareDate () {var oneday = new Date (); oneday. setFullYear (, 26); var currentday = new Date (); if (oneday





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.