• js获取n分钟(或n小时或n个月)后(或前)的时间(日期)


      标题有点绕,其实意思就是根据系统当前时间,获取n分钟或n小时或n个月后的时间。

      例如:当前时间下,获取10分钟后的时间。

    var date=new Date();     //1. js获取当前时间
    var min=date.getMinutes();  //2. 获取当前分钟
    date.setMinutes(min+10);  //3. 设置当前时间+10分钟:把当前分钟数+10后的值重新设置为date对象的分钟数
    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(formatdate) // 获取10分钟后的时间,格式为yyyy-mm-dd h:f:s
    

      同理,设置30分钟,60分钟或1个小时,5个小时,可以将小时转换为分钟,然后获取当前分钟再加上需要设置的时间即可。

      获取1个月后的日期:

    var date=new Date();
    date.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)

      同理获取n个月后或n个月前的日期都是如此

  • 相关阅读:
    monads-are-elephants(转)
    程序语言简史(转)
    语法的省略不能造成编译器的歧义
    scala getter and setter
    隐式类型转换
    java 调用 scala
    列表的操作
    Scala HandBook
    Scala 高级编程练习
    Net 2.0 C# 专用的只读类Tuple
  • 原文地址:https://www.cnblogs.com/jf-67/p/8659054.html
Copyright © 2020-2023  润新知