The correlation function of JS acquisition time and the conversion between time stamp and time date _javascript skill

Source: Internet
Author: User
Tags current time getdate local time

Time stamps and time-date conversions are common operations, and here are examples of how to implement the conversion between them through a code instance.

Before you learn this article, let's introduce the date () constructor parameters in javascript:

You must be familiar with the date object. Creating a Time object using the date () constructor is the most basic operation, such as:

var thedate=new Date ();
Thedate.getdate ();

Use the above code to get the day of the current date.

This is the simplest application for the date () constructor, which has several constructors, which are briefly listed below:

New Date () new date (milliseconds) new Date (datestring) new date (year
, month) new date
(year, month, day New Date (year, month, day, hours) new date (year,
month, day, hours, minutes)
new date (year, month, Day, ho Urs, minutes, seconds)
new Date (year, month, day, hours, minutes, seconds, microseconds)

The following is a simple analysis of the above several constructors.

1.new date (), when there are no parameters, the current time Date object is created.

2.new Date (milliseconds), when the parameter is a number, then this parameter is a timestamp, is treated as a millisecond, creating a time Date object that is specified in milliseconds from January 1, 1970.

3.new Date (datestring), this parameter is a string, and this string must be able to use Date.parse () conversion.

4. The following six constructors are precisely defined:

1. Year, is an integer, if it is 0-99, then add 1900 on this basis, the others are returned as is.
2). Month, is an integer, the range is 0-11.

3.day, is an integer, the range is 1-31.

4.hours, is an integer, the range is 0-23.

5.minutes, is an integer, the range is 0-59.

6.seconds, is an integer, the range is 0-59.

7.microseconds, is an integer, the range is 0-9999.

Code instance:

var d1=new Date ();
var d2=new Date (1320336000000);
var d3=new Date ("2013-8-20 18:20:30");
var d4=new Date (2013,7,26);

One. Time date converted to timestamp:

Now there's this one time date: "2013/5/12 20:10:20", convert it to a timestamp form:

The code is as follows:

var datestr= "2013/5/12 20:10:20";
var date=new date (DATESTR);

Use the gettime () function to get the timestamp of a specified time Date object.

Next we'll introduce the gettime () method of the JavaScript date object

Definition and usage of the GetTime () method:

This method returns the number of milliseconds between January 1, 1970 from the current time.

Note: This method needs to be invoked using a Date object.

Click to see more related Date object methods and properties.

Syntax structure:

Dateobject.gettime ()

Instance code:

var mydate=new Date ()
Console.log (Mydate.gettime ())

The above code can output the number of milliseconds between the current time from January 1, 1970.

In fact, there are many other ways, here is not introduced, just want to know this in the way can be, others can accumulate slowly.

Two. Timestamp converted to Event date:

Instance code one:

var date=new date (1368360620000);

The definition and usage of the tolocalestring () method toLocaleString () method for the Date object of javascript:

This method converts the Date object to a string based on local time, and returns the string.

Note: This method needs to be invoked with an instance of the Date object.

Syntax structure:

Dateobject.tolocalestring ()

Instance code:

var d=new Date ();
Console.log (D.tolocalestring ());

The toLocaleString () method of the Date object of JavaScript

Definition and usage of the toLocaleString () method:

This method converts the Date object to a string based on local time, and returns the string.

Note: This method needs to be invoked with an instance of the Date object.

Syntax structure:

Dateobject.tolocalestring ()

Instance code:

var d=new Date ();
Console.log (D.tolocalestring ());

Example two:

 function formatdate (now) {var year=now.getyear (); 
var month=now.getmonth () +1; 
var date=now.getdate (); 
var hour=now.gethours (); 
var minute=now.getminutes (); 
var second=now.getseconds (); 
return year+ "-" +month+ "-" +date+ "" +hour+ ":" +minute+ ":" +second; " 
var d=new Date (1368360620000); 
Console.log (FormatDate (d)); 

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.