js 日期处理

js 字符串转日期格式:

//日期格式化
function DateFormat(d) {
    var da = new Date(d.replace(/-/g, "/ "));//这句话网上搜到的,很多人都写错了。后面的/ 后面有个空格才是正确的方法
    return da.format("yyyy-MM-dd");//执行下面日期格式化的方法
}

js日期格式化:

Date.prototype.format = function(format) //author: meizz
{
    var o = {
        "M+": this.getMonth() + 1, //month
        "d+": this.getDate(),    //day
        "h+": this.getHours(),   //hour
        "m+": this.getMinutes(), //minute
        "s+": this.getSeconds(), //second
        "q+": Math.floor((this.getMonth() + 3) / 3),  //quarter
        "S": this.getMilliseconds() //millisecond
    }
    if (/(y+)/.test(format)) format = format.replace(RegExp.$1,
    (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o) if (new RegExp("(" + k + ")").test(format))
        format = format.replace(RegExp.$1,
      RegExp.$1.length == 1 ? o[k] :
        ("00" + o[k]).substr(("" + o[k]).length));
    return format;
}

用法:

var d="2012-12-12 12:12:12";

var da=new Date(d.replace(/-/g, "/ "));//js字符串转日期!!!!!!!!!!!!!!!

alert(new Date().format("yyyy-MM-dd"));
alert(new Date("january 12 2008 11:12:30").format("yyyy-MM-dd hh:mm:ss"));

 js字符串转转换为日期类型

 把一个日期字符串如“2007-2-28 10:18:30”转换为Date对象:

var   s   =   "2005-12-15   09:41:30";   
var   d   =   new   Date(Date.parse(s.replace(/-/g,   "/")));   

 JS比较两个日期的大小

原文地址:http://blog.csdn.net/lingyu2843/article/details/4799289

//功能:比较开始日期和结束日期的大小  
  //参数:obj:当前对象  
//startTimeId:开始日期文本ID,  
  //endTimeId:结束日期文本框ID  
 function compareDate(obj, startTimeId, endTimeId) {  
     var endTime = $("#" + endTimeId).val();  
     if (endTime != "") {  
         var startTime = $("#" + startTimeId).val();  
         if (startTime != "") {  
             var sTimeA = startTime.split('-');  
             var eTimeA = endTime.split('-');  
             var maxDate = new Date(eTimeA[0], eTimeA[1], eTimeA[2]);  
             var minDate = new Date(sTimeA[0], sTimeA[1], sTimeA[2]);  
             if (maxDate < minDate) {  
                 $(obj).val("");    //清空当前对象文本框内容  
                 alert("开始日期不能大于结束日期");  
             }  
         }  
     }  
 }  

 JS获取当前日期 时分秒 格式 YYYY-MM-dd hh:mm:ss

var getCurrentDateTime = function() {  
    //获取当前时间
    var now= new Date();
    var _month = ( 10 > (now.getMonth()+1) ) ? '0' + (now.getMonth()+1) : now.getMonth()+1;
    var _day = ( 10 > now.getDate() ) ? '0' + now.getDate() : now.getDate();
    var _hour = ( 10 > now.getHours() ) ? '0' + now.getHours() : now.getHours();
    var _minute = ( 10 > now.getMinutes() ) ? '0' + now.getMinutes() : now.getMinutes();
    var _second = ( 10 > now.getSeconds() ) ? '0' + now.getSeconds() : now.getSeconds();
    return now.getYear() + '-' + _month + '-' + _day + ' ' + _hour + ':' + _minute + ':' + _second;
}

js求时间差

var date1=new Date();  //开始时间
alert("aa");
var date2=new Date();    //结束时间
var date3=date2.getTime()-date1.getTime()  //时间差的毫秒数

 
//计算出相差天数
var days=Math.floor(date3/(24*3600*1000))
 

//计算出小时数
var leave1=date3%(24*3600*1000)    //计算天数后剩余的毫秒数
var hours=Math.floor(leave1/(3600*1000))
//计算相差分钟数
var leave2=leave1%(3600*1000)        //计算小时数后剩余的毫秒数
var minutes=Math.floor(leave2/(60*1000))

 
//计算相差秒数
var leave3=leave2%(60*1000)      //计算分钟数后剩余的毫秒数
var seconds=Math.round(leave3/1000)

 
alert(" 相差 "+days+"天 "+hours+"小时 "+minutes+" 分钟"+seconds+" 秒")
原文地址:https://www.cnblogs.com/wangjunwei/p/2680278.html