jquery Get computer time, convert time format

Source: Internet
Author: User
Tags current time getdate

An effect written yesterday needs to get the current time of the computer, because it has been forgotten for so long. Review:

1. Get computer time

var mydate = new Date ();
Mydate.getyear (); Get Current year (2-bit)
Mydate.getfullyear (); Get the full year (4-bit, 1970-????)
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
var mytime=mydate.tolocaletimestring (); Get current time
Mydate.tolocalestring (); Get Date and time


This is the time to get the current computer, to yy-dd-hh this form of rendering:

var date = new Date (+new date () +8*3600*1000). Toisostring (). Replace (/t/g, "). Replace (/\.[ \d]{3}z/, ");

This is the current time to push backwards one day:
var now = new Date ();
Now.setdate (Now.getdate () +1);


This is the date format that was pushed up the day to the standard format:

var d = new Date (now);
var youwant =d.getfullyear () + '-' + (D.getmonth () + 1) + '-' + d.getdate () + ' + d.gethours () + ': ' + d.getminutes () + ': ' + d.getseconds ();


Reprint Address: http://www.cnblogs.com/LiuJL/p/5417685.html


2. Convert Computer time format

Sometimes the project will use the JS date format, because the format returned by date () is not what we need.

Date () returns the format:

Thu Mar 12:00:00 gmt+0800 (China Standard Time)

And we need this format:

2015-3-19 12:00:00

2-1. js method return Value: 2015-03-19

var formatdate = function (date) {var y = date.getfullyear ();       var m = date.getmonth () + 1; m = m < 10?       ' 0 ' + m:m;       var d = date.getdate (); D = d < 10?       (' 0 ' + D): D;   Return y + '-' + M + '-' + D; };
2-2.js method return value: 2015-03-19 12:00

var formatdatetime = function  (date)  {       var y  = date.getfullyear ();       var m = date.getmonth ()  + 1;       m = m < 10 ?  (' 0 '  + &NBSP;M)  : m;       var d = date.getdate ();       d = d < 10 ?  (' 0 '  + d)  : d;        var h = date.gethours ();       var  minute = date.getminutes ();       minute = minute  < 10 ?  (' 0 '  + minute)  : minute;        return y +  '-'  + m +  '-'  + d+ '   ' +h+ ': ' +minute;  };  

Call: FormatDate (date ()) or formatdate (date ()).

2-3. How to convert 2015-03-12 12:00 to Standard Time ().

Thu Mar 12:00:00 gmt+0800 (China Standard Time)
JS method return Value: Thu Mar 12:00:00 gmt+0800 (China Standard Time)

var parserdate = function (date) {var t = date.parse (date);       if (!isnan (t)) {return new Date (Date.parse (Date.replace (/-/g, "/"));       } else {return new Date ();   }   }; Call: Parserdate ("2015-03-19 12::00:00").

Reprint Address: http://blog.csdn.net/lilinoscar/article/details/44459571


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.