获取日期,实时显示当前时间,时间相减

直接获取时间:用到  format()方法

var date= new Date().format("yyyy-MM-dd");

js获取当前日期时间  格式:yyyy-mm-dd hh:mm:ss

 1 function getNowFormatDate() {
 2      var date = new Date();
 3 var sign1 = "-";
 4 var sign2 = ":";
 5 var year = date.getFullYear() //
 6 var month = date.getMonth() + 1; //
 7 var day = date.getDate(); //
 8       var hour = date.getHours(); //
 9 var minutes = date.getMinutes(); //
10 var seconds = date.getSeconds() //
11 var weekArr = ['星期一', '星期二', '星期三', '星期四', '星期五', '星期六', '星期天'];
12 var week = weekArr[date.getDay()];
13 // 给一位数数据前面加 “0”
14 if (month >= 1 && month <= 9) {
15 month = "0" + month;
16 }
17 if (day >= 0 && day <= 9) {
18 day = "0" + day;
19 }
20 if (hour >= 0 && hour <= 9) {
21 hour = "0" + hour;
22 }
23 if (minutes >= 0 && minutes <= 9) {
24 minutes = "0" + minutes;
25 }
26 if (seconds >= 0 && seconds <= 9) {
27 seconds = "0" + seconds;
28 }
29 var currentdate = year + sign1 + month + sign1 + day + " " + hour + sign2 + minutes + sign2 + seconds + " " + week;
30 return currentdate;
31 
32 }
33 
34 console.log(getNowFormatDate()); //2018-08-06 09:56:57 星期二

js中格式化日期,调用的时候直接用
//js中格式化日期,调用的时候直接:new Date().format("yyyy-MM-dd hh:mm:ss")
    Date.prototype.format = function(fmt) {
        var o = {    
            "M+": this.getMonth() + 1,
                     //月份 
                "d+": this.getDate(),
                       //日 
                "h+": this.getHours(),
                      //小时 
                "m+": this.getMinutes(),
                     //分 
                "s+": this.getSeconds(),
                     //秒 
                "q+": Math.floor((this.getMonth() + 3) / 3), //季度
                "S" : this.getMilliseconds()       //毫秒 
                    
        };         
        if(/(y+)/.test(fmt)) {    
            fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length)); 
        }
        for(var k in o) {    
            if(new RegExp("(" + k + ")").test(fmt)) {        
                fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));            
            }                
        }        
        return fmt;         
    }

 

//js实时获取并前显示当前时间
$(document).ready(function() {
  function time() {
    var date = new Date();
    var n = date.getFullYear();
    var y = date.getMonth()+1;
    var t = date.getDate();
    var h = date.getHours();
    var m = date.getMinutes();
    var s = date.getSeconds();
    var today = new Array('星期日','星期一','星期二','星期三','星期四','星期五','星期六');
    var week = today[date.getDay()];

    $('.headtimes span').eq(0).html(n+"-"+y+"-"+t);
    $('.headtimes span').eq(1).html(week);
    $('.headtimes span').eq(2).html(h+":"+ (m<10 ? '0' : '') + m +":"+(s<10 ? '0' : '') + s);
    for (var i = 0; i < $('div').length; i++) {
      if ($('div').eq(i).text().length == 1) {
        $('div').eq(i).html(function(index, html) {
          return 0 + html;
        });
      }
    }
  }
  time();
  setInterval(time, 1000);
});

 

html:

<div class='headtimes'>
  <span></span>
  <span></span>
  <span></span>
</div>

 

 

 

//时间差:先转成时间戳 减后再转成时间(如:获取相差天数)   (相减后的时间)/(24*60*60*1000)

方法一:

js中时间与日期格式的转换:

1. 将时间戳转换成日期格式:格式:yyyy-mm-dd hh:mm:ss

function timestampToTime(timestamp) {

  var date = new Date(timestamp * 1000);//时间戳为10位需*1000,时间戳为13位的话不需乘1000

  Y = date.getFullYear() + '-';

  M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';

  D = date.getDate() + ' ';

  h = date.getHours() + ':';

  m = date.getMinutes() + ':';

  s = date.getSeconds();

  return Y+M+D+h+m+s;

}
  timestampToTime(1532476834);
  console.log(timestampToTime(1532476834));//yyyy-mm-dd hh:mm:ss

 

 
2.将日期格式转换成时间戳:
注意:获取到的时间戳除以1000就可获得Unix时间戳,就可传值给后台得到。
var date = new Date('2018-07-25');
 // 有三种方式获取
 var time1 = date.getTime();
 var time2 = date.valueOf();
 var time3 = Date.parse(date);
 console.log(time1);//1532476800000//会精确到毫秒
 console.log(time2);//1532476800000//会精确到毫秒
 console.log(time3);//1532476800000//只能精确到秒,毫秒用000替代

 方法二:(2018-02-03 到2018-11-33 相差天数

            function getDateStr(obj){
                    var s,y,m,d,str,a,b,a1,b1;
                    s = new Date();
                    y = s.getFullYear();
                    m = s.getMonth()+1;
                    d = s.getDate();
                    if(m >= 1 && m <= 9) m = '0' + m;
                    if(d >= 1 && d <= 9) d = '0' + d;
                    str = y+'-'+m+'-'+d;

                    a = str.split('-');
                    b = obj.split('-');
                    a1 = new Date(a[1]+'-'+a[2]+'-'+a[0]);
                    b1 = new Date(b[1]+'-'+b[2]+'-'+b[0]);
                    var Days = parseInt(Math.abs(a1-b1)/1000/60/60/24);
                    return Days;
                }    

var DateStart = new Date("2018-02-03");
var DateEnd = new Date("2018-03-04");
var days = (DateEnd.getTime() - DateStart.getTime()) / 24 / 60 / 60 / 1000;

 

 


 

posted @ 2018-07-25 09:46  小凢  阅读(1319)  评论(0编辑  收藏  举报