JS gets n minutes (or n hours or n months) after (or before) the time (date)

Source: Internet
Author: User

The title is a bit around, in fact, it means that according to the current time of the system, get n minutes or n hours or n months after the time.

For example: Under current time, get 10 minutes after the time.

var date=new date ();     1. JS gets the current time Var min=date.getminutes ();  2. Gets the current minute date.setminutes (min+10);  3. Set current time + 10 minutes: Reset the value after the current number of minutes to the number of minutes of the date object var y = date.getfullyear (), var m = (Date.getmonth () + 1) < 10? ("0" + (date.getmonth () + 1)): (Date.getmonth () + 1); var d = date.getdate () < 10? ("0" + date.getdate ()): Date.getdate (); var h = date.gethours () < 10? (' 0 ' + date.gethours ()): Date.gethours () var f = date.getminutes () < 10? (' 0 ' + date.getminutes ()): Date.getminutes () var s = date.getseconds () < 10? (' 0 ' + date.getseconds ()): Date.getseconds () var formatdate = y+ '-' +m+ '-' +d + ' "+ H +": "+ F +": "+ s;console.log (Form atdate)//Get 10 minutes after the time, formatted as YYYY-MM-DD h:f:s

Similarly, set 30 minutes, 60 minutes or 1 hours, 5 hours, you can convert the hour to minutes, and then get the current minute plus the time you need to set.

Get the date after 1 months:

var date=new date ();d Ate.setmonth (Date.getmonth () +1), var y = date.getfullyear (), var m = (Date.getmonth () + 1) < 10? ("0" + (date.getmonth () + 1)): (Date.getmonth () + 1); var d = date.getdate () < 10? ("0" + date.getdate ()): Date.getdate (); var h = date.gethours () < 10? (' 0 ' + date.gethours ()): Date.gethours () var f = date.getminutes () < 10? (' 0 ' + date.getminutes ()): Date.getminutes () var s = date.getseconds () < 10? (' 0 ' + date.getseconds ()): Date.getseconds () var formatwdate = y+ '-' +m+ '-' +d + ' "+ H +": "+ F +": "+ S; Console.log (' formatwdate ', formatwdate)

Similarly get n months or n months before the date is so

JS gets n minutes (or n hours or n months) after (or before) the time (date)

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.