不管是设置相对当前时间有多久时间差的时间,思路:先获取当前时间的时间戳,再根据需求加减时间获得新的时间戳,然后取年月日与时分秒。实例:

// 设置默认时间——先转化为毫秒数,加上 24 小时的毫秒数,再转化回来
function setTime () {
let t = new Date().getTime() + 24 * 60 * 60 * 1000; //24小时 * 60分钟 * 60秒 * 1000
let d = new Date(t);
let theMonth = d.getMonth() + 1;
let theDate = d.getDate();
let theHours = d.getHours();
let theMinutes = d.getMinutes();
if (theMonth < 10) {
theMonth = '0' + theMonth
}
if (theDate < 10) {
theDate = '0' + theDate
}
if (theHours < 10) {
theHours = '0' + theHours
}
if (theMinutes < 10) {
theMinutes = '0' + theMinutes
}
let date = d.getFullYear() + '-' + theMonth + '-' + theDate
let time = theHours + ':' + theMinutes
let Spare = date + ' ' + time
console.log(date)
console.log(time)
console.log(Spare)
}

js 设置当前时间的后24小时、后一小时等相对时间的更多相关文章

  1. mysql连接的空闲时间超过8小时后 MySQL自动断开该连接解决方案

    在连接字符串中  添加设置节点 ConnectionLifeTime(计量单位为 秒).超过设定的连接会话 会被杀死! Connection Lifetime, ConnectionLifeTime ...

  2. js 实现几分钟前、几小时前、几天前,以及几分钟后、几小时后、几天前后

    js 实现几分钟前.几小时前.几天前,以及几分钟后.几小时后.几天前后 /* * * 把传入的时间戳与当前时间比较,计算几分钟前.几小时前.几天前,以及几分钟后.几小时后.几天前后 * unixtim ...

  3. 【代码笔记】iOS-密码在进入后台1小时后重新设置

    代码: AppDelegate.m #import "AppDelegate.h" #import "ViewController.h" @interface ...

  4. JFreeChart时间轴固定24小时每天刷新

    Timeseries时间轴,设置x轴固定长度24小时 xAxis.setFixedAutoRange(3600000 * 2D), 再画出当天24点这一点 Date day = new Date(); ...

  5. js设置下拉框选中后change事件无效解决

    下拉框部分代码: <select id="bigType"> <option value="">请选择</option> & ...

  6. Android系统改动时间格式为24小时制

    1. frameworks/base/packages/SettingsProvider/res/values/defaults.xml 添加<stringname="time_12_ ...

  7. js设置时间无效的问题

    在发送短信息验证码的时候要用到js设置时间倒序问题:有时候这种常规写法会导致js失效,试了很多方法才找到问题所在,可能是因为js版本过低导致. setTimeout(showT(t-1),5000) ...

  8. 运用JS设置cookie、读取cookie、删除cookie

    JavaScript是运行在客户端的脚本,因此一般是不能够设置Session的,因为Session是运行在服务器端的.而cookie是运行在客户端的,所以可以用JS来设置cookie. 假设有这样一种 ...

  9. JS设置cookie

    cookie 与 session 是网页开发中常用的信息存储方式.Cookie是在客户端开辟的一块可存储用户信息的地方:Session是在服务器内存中开辟的一块存储用户信息的地方. JavaScrip ...

  10. JS设置cookie、读取cookie、删除cookie(转载)

    JavaScript是运行在客户端的脚本,因此一般是不能够设置Session的,因为Session是运行在服务器端的.而cookie是运行在客户端的,所以可以用JS来设置cookie.假设有这样一种情 ...

随机推荐

  1. JetBrains 系列软件--操作数据库+centos系统

    这系列软件贼强大! 能操作数据库 也能操作centos(linux)系统 由于这系列都有这两个功能,下面以最近常用的JetBrains PhpStorm 2017.2.1 x64来举例子: 一.操作数 ...

  2. oracle-Mount

    执行nomount的所有工作,另外附加数据结构并与这些数据结构进行交互.这时,oracle从控制文件中获得信息. 可以执行的任务是: 执行数据库的完全恢复操作 重命名数据文件 改变数据库的归档状态. ...

  3. getElementsByClassName兼容低版本浏览器

    var getElementsByClassName = function (searchClass, node,tag) { if(document.getElementsByClassName){ ...

  4. 【风马一族_php】PHP运算

    运算 算术运算符 <?php //加法 $num1 = 10; $num2 = 43; echo $num1 + $num2; echo " "; var_dump($num ...

  5. SQL语法之DDL和DML

    SQL语法之DDL和DML        DDL数据库定义语言 create 创建 alter 修改 drop 删除 drop和delete的区别 truncate DML 数据操作语言 insert ...

  6. 利用IDEA构建springboot应用-Controller的使用

    Controller的使用 @Controller 处理http请求   @RestController  Spring4之后新加的注解,原来返回json需要@ResponseBody配合@Contr ...

  7. FZU 2234 牧场物语【多线程dp】

     Problem 2234 牧场物语  Problem Description 小茗同学正在玩牧场物语.该游戏的地图可看成一个边长为n的正方形. 小茗同学突然心血来潮要去砍树,然而,斧头在小茗的右下方 ...

  8. python 替换字符串的方法replace()、正则re.sub()

    一.replace()函数1用字符串本身的replace方法: a = 'hello word' b = a.replace('word','python') print b   1 2 3 二.re ...

  9. POJ-3615_Cow Hurdles

    Cow Hurdles Time Limit: 1000MS Memory Limit: 65536K Description Farmer John wants the cows to prepar ...

  10. Java练习 SDUT-1239_水仙花数

    水仙花数 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 春天是鲜花的季节,水仙花就是其中最迷人的代表,数学上有个水仙花 ...