于春停吧 关注:42贴子:322
  • 5回复贴,共1

Js获取当前日期时间及其它操作

只看楼主收藏回复

var myDate = new Date();
myDate.getYear(); //获取当前年份(2位)
myDate.getFullYear(); //获取完整的年份(4位,1970-????)
myDate.getMonth(); //获取当前月份(0-11,0代表1月)
myDate.getDate(); //获取当前日(1-31)
myDate.getDay(); //获取当前星期X(0-6,0代表星期天)
myDate.getTime(); //获取当前时间(从1970.1.1开始的毫秒数)
myDate.getHours(); //获取当前小时数(0-23)
myDate.getMinutes(); //获取当前分钟数(0-59)
myDate.getSeconds(); //获取当前秒数(0-59)
myDate.getMilliseconds(); //获取当前毫秒数(0-999)
myDate.toLocaleDateString(); //获取当前日期
var mytime=myDate.toLocaleTimeString(); //获取当前时间
myDate.toLocaleString( ); //获取日期与时间


IP属地:北京1楼2015-04-16 17:55回复
    //+---------------------------------------------------
    //| 求两个时间的天数差 日期格式为 YYYY-MM-dd
    //+---------------------------------------------------
    function daysBetween(DateOne,DateTwo)
    {
    var OneMonth = DateOne.substring(5,DateOne.lastIndexOf ('-'));
    var OneDay = DateOne.substring(DateOne.length,DateOne.lastIndexOf ('-')+1);
    var OneYear = DateOne.substring(0,DateOne.indexOf ('-'));
    var TwoMonth = DateTwo.substring(5,DateTwo.lastIndexOf ('-'));
    var TwoDay = DateTwo.substring(DateTwo.length,DateTwo.lastIndexOf ('-')+1);
    var TwoYear = DateTwo.substring(0,DateTwo.indexOf ('-'));
    var cha=((Date.parse(OneMonth+'/'+OneDay+'/'+OneYear)- Date.parse(TwoMonth+'/'+TwoDay+'/'+TwoYear))/86400000);
    return Math.abs(cha);
    }
    //+---------------------------------------------------
    //| 日期计算
    //+---------------------------------------------------
    Date.prototype.DateAdd = function(strInterval, Number) {
    var dtTmp = this;
    switch (strInterval) {
    case 's' :return new Date(Date.parse(dtTmp) + (1000 * Number));
    case 'n' :return new Date(Date.parse(dtTmp) + (60000 * Number));
    case 'h' :return new Date(Date.parse(dtTmp) + (3600000 * Number));
    case 'd' :return new Date(Date.parse(dtTmp) + (86400000 * Number));
    case 'w' :return new Date(Date.parse(dtTmp) + ((86400000 * 7) * Number));
    case 'q' :return new Date(dtTmp.getFullYear(), (dtTmp.getMonth()) + Number*3, dtTmp.getDate(), dtTmp.getHours(), dtTmp.getMinutes(), dtTmp.getSeconds());
    case 'm' :return new Date(dtTmp.getFullYear(), (dtTmp.getMonth()) + Number, dtTmp.getDate(), dtTmp.getHours(), dtTmp.getMinutes(), dtTmp.getSeconds());
    case 'y' :return new Date((dtTmp.getFullYear() + Number), dtTmp.getMonth(), dtTmp.getDate(), dtTmp.getHours(), dtTmp.getMinutes(), dtTmp.getSeconds());
    }
    }
    //+---------------------------------------------------
    //| 比较日期差 dtEnd 格式为日期型或者有效日期格式字符串
    //+---------------------------------------------------
    Date.prototype.DateDiff = function(strInterval, dtEnd) {
    var dtStart = this;
    if (typeof dtEnd == 'string' )//如果是字符串转换为日期型
    {
    dtEnd = StringToDate(dtEnd);
    }
    switch (strInterval) {
    case 's' :return parseInt((dtEnd - dtStart) / 1000);
    case 'n' :return parseInt((dtEnd - dtStart) / 60000);
    case 'h' :return parseInt((dtEnd - dtStart) / 3600000);
    case 'd' :return parseInt((dtEnd - dtStart) / 86400000);
    case 'w' :return parseInt((dtEnd - dtStart) / (86400000 * 7));
    case 'm' :return (dtEnd.getMonth()+1)+((dtEnd.getFullYear()-dtStart.getFullYear())*12) - (dtStart.getMonth()+1);
    case 'y' :return dtEnd.getFullYear() - dtStart.getFullYear();
    }
    }


    IP属地:北京3楼2015-04-16 17:56
    回复
      渣渣


      来自Android客户端6楼2015-04-17 08:21
      收起回复


        来自iPhone客户端7楼2015-05-28 23:22
        收起回复