JS obtains time-related functions and converts timestamp to time and date. js obtains

Source: Internet
Author: User
Tags time and date timestamp to date

JS obtains time-related functions and converts timestamp to time and date. js obtains

Timestamp and date conversion are common operations. The following describes how to implement mutual conversion through code examples.

Before learning this article, I will introduce the Date () constructor parameters in javascript:

You must be familiar with the Date object. Using the Date () constructor to create a time object is the most basic operation. For example:

var theDate=new Date();theDate.getDate();

Use the above Code to obtain the day of the current date.

The above is the simplest application of the Date () constructor. The Date object has multiple constructor types. The following is a simple example:

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, hours, minutes, seconds)
new Date(year, month, day, hours, minutes, seconds, microseconds)

The following is a simple analysis of the above constructors.

1. new Date (). When there is no parameter, the object is created at the current time and Date.

2. new Date (milliseconds). When the parameter is a number, this parameter is a timestamp, which is regarded as millisecond. A Date object created from the specified millisecond on January 1, January 1, 1970 is created.

3. new Date (datestring). This parameter is a string and must be converted using Date. parse.

4. The following six constructors are precisely defined:

1). year is an integer. If it is 0-99, 1900 is added to the base, and all others are returned as is.
2). month is an integer ranging from 0 to 11.

3. day is an integer in the range of 1-31.

4. hours is an integer ranging from 0 to 23.

5. minutes is an integer in the range of 0-59.

6. seconds is an integer in the range of 0-59.

7. microseconds, which is an integer in the range of 0 to 9999.

Code example:

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);

1. convert a time date to a timestamp:

Now there is a time Date: "20:10:20", which is converted to the timestamp format:

The Code is as follows:

var dateStr="2013/5/12 20:10:20";
var date=new Date(dateStr);
console.log(date.getTime()); 

You can use the getTime () function to obtain the timestamp of the specified time and date object.

Next we will introduce the getTime () method of the Date object in javascript.

GetTime () method definition and usage:

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

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

For more information about Date object methods and attributes, see.

Syntax structure:

DateObject. getTime ()

Instance code:

var myDate=new Date()console.log(myDate.getTime())

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

In fact, there are many other methods. We will not introduce them here. You only need to know the method in this article. You can accumulate other methods by yourself.

2. Time Stamp conversion to Event Date:

Example code 1:

var date=new Date(1368360620000);console.log(date.toLocaleString()); 

The toLocaleString () method of the Date object in javascript defines and uses the toLocaleString () method:

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

Note: This method needs to be called by the Date object instance.

Syntax structure:

DateObject. toLocaleString ()

Instance code:

var theDate=new Date();
theDate.getDate();

The toLocaleString () method of the javascript Date object

ToLocaleString () method definition and usage:

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

Note: This method needs to be called by the Date object instance.

Syntax structure:

DateObject. toLocaleString ()

Instance code:

var d=new Date();console.log(d.toLocaleString());

Example 2:

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));
Articles you may be interested in:
  • Javascript date conversion timestamp to date format
  • JavaScript code that converts a timestamp to a date format
  • Small Example of converting a date into a timestamp in javascript
  • Multiple methods for formatting js timestamps into date formats
  • How to process timestamp in javascript in Date Format
  • Conversion functions of JavaScript datetime and timestamp
  • How to convert js timestamp to date format


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.