收起左侧

[前端] js获取当前时间前一个小时的方法

0
回复
[复制链接]

4

主题

-1

回帖

64

积分

发表于 2019-6-3 15:48:44 | 显示全部楼层 |阅读模式
1,新建js文件,复制粘贴以下内容
  1. $(function(){

  2.         Date.prototype.format = function(fmt) {
  3.             var o = {
  4.                 "M+": this.getMonth() + 1, //月份
  5.                 "d+": this.getDate(), //日
  6.                         "h+": this.getHours(), //小时
  7.                         "m+": this.getMinutes(), //分
  8.                         "s+": this.getSeconds(), //秒
  9.                         "q+": Math.floor((this.getMonth() + 3) / 3), //季度
  10.                         "S": this.getMilliseconds() //毫秒
  11.                 };
  12.                 if(/(y+)/.test(fmt)) {
  13.                         fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 -RegExp.$1.length));
  14.                 }
  15.                 for(var k in o) {
  16.                         if(new RegExp("(" + k + ")").test(fmt)) {
  17.                             fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
  18.                         }
  19.                 }
  20.                 return fmt;
  21.         }
  22.         
  23.         
  24.         var now = new Date;
  25.         now.setMinutes(now.getMinutes() - 60);
  26.         var stime = now.format("yyyy-MM-dd hh:mm");//一个小时前的时间
  27.         var etime = new Date().format("yyyy-MM-dd hh:mm:ss");//当前时间
  28.         $(".hours").text(stime);
  29. //        $(".hours").val(etime);
  30. //        console.log(etime)
  31. })
复制代码


2,在html中引入:
     <script src="js路径" type="text/javascript" charset="utf-8"></script>
     src路径根据你文件夹目录而定
3,在你需要修改的时间标签上加入class="hours"
4,ok,时间修改完成
参与人数 1金币 +120 收起 理由
itjc8 + 120 感谢分享!

查看全部评分总评分 : 金币 +120

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则