The acquisition of various JS timestamps, the acquisition of various time formats (a chicken on the way to the attack)

Source: Internet
Author: User

1. Into the front of the army for some time, always feel the lack of something, every day to knock business code, in addition to copy and paste other people's thoughts, the rest of their own things really did not. I think it is necessary to write down something, I have to leave some things and memories of ourselves ...

No more nonsense, start to the point:

JS Acquisition Time:

JavaScript gets the current timestamp:

The first method:

var timestamp = date.parse (new Date ());

Results: 1280977330000
The second method:

var timestamp = (new Date ()). ValueOf ();

Results: 1280977330748

The third method:

var timestamp=new Date (). GetTime ();
Results: 1280977330748

The first: The time stamp obtained is the change of milliseconds to 000 display,
The second and third is to get the timestamp of the current millisecond.

Alternative method gets timestamp:

IE8 above can be used directly using the Date.now () method

IE8 the following versions
if (! Date.now) {
Date.now = function () {return new Date (). GetTime ();};
}

JQuery get timestamp $.now ()

var timestamp = $.now ();

JS Get other Time:

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

Common conversion methods:

/**** JS conversion timestamp to YYYY-MM-DD, put--change to/that is yyyy/mm/dd***/

function Getnowformatdate (s)
{
var day = new Date (s);
var year = 0;
var Month = 0;
var day = 0;
var currentdate = "";
Initialization time
Year= day.getyear ();//have Firefox next 2008 years show 108 bug
Year= day.getfullyear ();//ie Firefox can
Month= day.getmonth () +1;
Day = Day.getdate ();
Hour = Day.gethours ();
Minute = Day.getminutes ();
Second = Day.getseconds ();
CurrentDate + = year + "-";
if (Month >= 10)
{
currentdate + = Month + "-";
}
Else
{
currentdate + = "0" + Month + "-";
}
if (day >=)
{
currentdate + = day;
}
else
{
currentdate + = "0" + day;
}
return currentdate;
}

The acquisition of various JS timestamps, the acquisition of various time formats (a chicken's way of attack)

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.