zoukankan      html  css  js  c++  java
  • js 里时间转换util

    /**
    * unix 时间转化成 YYYY-MM-dd hh:mm:ss
    */
    function unixTimeToStr(unixTime){

    var ymd=new Date();
    ymd.setTime(unixTime*1000);
    var year=ymd.getFullYear().toString();
    var month=(ymd.getMonth()+1).toString();

    var date=ymd.getDate().toString();
    var hour=ymd.getHours().toString();
    var minute=ymd.getMinutes().toString();
    var second=ymd.getSeconds().toString();

    if( month.length == 1 ){
    month = "0" + month;
    }
    if( date.length == 1 ){
    date = "0" + date;
    }
    if( hour.length == 1 ){
    hour = "0" + hour;
    }
    if( minute.length == 1 ){
    minute = "0" + minute ;
    }
    if(second.length == 1 ){
    second = "0" + second;
    }
    var time =year+'-'+month+'-'+date+' '+hour+':'+minute+':'+second;
    return time;
    }

    /**
    * unix 时间转化成 YYYY-MM-dd hh:mm
    * @param unixTime
    * @returns {String}
    */
    function unixTimeToMinStr(unixTime){

    var ymd=new Date();
    ymd.setTime(unixTime*1000);
    var year=ymd.getFullYear().toString();
    var month=(ymd.getMonth()+1).toString();

    var date=ymd.getDate().toString();
    var hour=ymd.getHours().toString();
    var minute=ymd.getMinutes().toString();
    var second=ymd.getSeconds().toString();

    if( month.length == 1 ){
    month = "0" + month;
    }
    if( date.length == 1 ){
    date = "0" + date;
    }
    if( hour.length == 1 ){
    hour = "0" + hour;
    }
    if( minute.length == 1 ){
    minute = "0" + minute ;
    }

    var time =year+'-'+month+'-'+date+' '+hour+':'+minute;
    return time;
    }


    /**
    * unix 时间转化成 MM-dd hh:mm
    * @param unixTime
    * @returns {String}
    */
    function unixTimeToShortStr(unixTime){

    var ymd=new Date();
    ymd.setTime(unixTime*1000);
    var month=(ymd.getMonth()+1).toString();
    var date=ymd.getDate().toString();
    var hour=ymd.getHours().toString();
    var minute=ymd.getMinutes().toString();

    if( month.length == 1 ){
    month = "0" + month;
    }
    if( date.length == 1 ){
    date = "0" + date;
    }
    if( hour.length == 1 ){
    hour = "0" + hour;
    }
    if( minute.length == 1 ){
    minute = "0" + minute ;
    }
    var time = month+'-'+date+' '+hour+':'+minute;
    return time;
    }

    /**
    * 日期转换成unxi时间戳
    * @param dateStr
    * @returns {Number}
    */
    function getUnixTime(dateStr)
    {
    /* var newstr = dateStr.replace(/-/g,'/');
    var date = new Date(newstr);
    var time_str = date.getTime().toString();
    return time_str.substr(0, 10);*/

    dateStr = dateStr.replace(/-/g,'/');
    var timestamp = new Date(dateStr).getTime()/1000;
    return timestamp;
    }

    /**
    * 获取当前日期unxi时间戳
    * @returns unix时间戳
    */
    function getCurrentDateUnixTime(){
    var oDate = new Date(); // 实例一个时间对象
    var year = oDate.getFullYear();
    var month = oDate.getMonth()+1;
    var day = oDate.getDate();
    var nowDate = year +"-"+ month + "-"+ day +" 00:00:00" ;
    return getUnixTime(nowDate);
    }

    /**
    * 获取前几天的日期 格式: yyyy-MM-dd
    * @param 往前推算天数
    * @returns unix时间戳
    */
    function getBeforeDateUnixTime(n){
    var n = n;
    var d = new Date();
    var year = d.getFullYear();
    var mon=d.getMonth()+1;
    var day=d.getDate();
    if(day <= n){
    if(mon>1) {
    mon=mon-1;
    }
    else {
    year = year-1;
    mon = 12;
    }
    }
    d.setDate(d.getDate()-n);
    year = d.getFullYear();
    mon=d.getMonth()+1;
    day=d.getDate();
    s = year+"-"+(mon<10?('0'+mon):mon)+"-"+(day<10?('0'+day):day);
    s +=" 00:00:00";
    return getUnixTime(s);
    }


    /**
    * unix 时间转化成 YYYY年MM月dd日
    */
    function unixTimeToDate(unixTime){
    var ymd=new Date();
    ymd.setTime(unixTime*1000);
    var year=ymd.getFullYear().toString();
    var month=(ymd.getMonth()+1).toString();
    var date=ymd.getDate().toString();
    var nowDate = year+"年"+month+"月"+date+"日";
    return nowDate;
    }

    //计算天数差的函数,通用
    function DateDiff(sDate1, sDate2){
    //sDate1和sDate2是2002-12-18格式
    var aDate, oDate1, oDate2, iDays ;
    var ua = navigator.userAgent.toLowerCase();
    if(ua.indexOf("chrome") > -1){
    aDate = sDate1.split("-")
    oDate1 = new Date(aDate[1] + '-' + aDate[2] + '-' + aDate[0]) //转换为12-18-2002格式
    aDate = sDate2.split("-")
    oDate2 = new Date(aDate[1] + '-' + aDate[2] + '-' + aDate[0])
    iDays = parseInt(Math.abs(oDate1 - oDate2) / 1000 / 60 / 60 /24) //把相差的毫秒数转换为天数
    }
    else{

    aDate = sDate1.split("-") ;
    oDate1 = new Date(aDate[0] + '/' + aDate[1] + '/' + aDate[2]) ; //转换为12-18-2002格式
    aDate = sDate2.split("-") ;
    oDate2 = new Date(aDate[0] + '/' + aDate[1] + '/' + aDate[2]) ;
    iDays = parseInt(Math.abs(oDate1 - oDate2) / 1000 / 60 / 60 /24); //把相差的毫秒数转换为天数
    }
    return iDays
    }

    /*获取日期的年、月、日*/
    function fortime(date){
    var date = new Date(date);
    var y = date.getFullYear();
    var m = date.getMonth()+1;
    var d = date.getDate();
    var nowDate = y +"-"+ m + "-"+ d ;
    return nowDate;
    }

  • 相关阅读:
    第一个程序01 零基础入门学习汇编语言20
    函数03 零基础入门学习C语言34
    函数03 零基础入门学习C语言34
    寄存器(内存访问)05 零基础入门学习汇编语言17
    寄存器(内存访问)06 零基础入门学习汇编语言18
    寄存器(内存访问)07 零基础入门学习汇编语言19
    寄存器(内存访问)06 零基础入门学习汇编语言18
    VC++实现恢复SSDT
    现实世界Windows Azure: 排气系统制造商 Akrapovič Revs 运用Windows Azure进行全球运营
    VC++实现全局钩子勾住消息对话框
  • 原文地址:https://www.cnblogs.com/missmeng/p/12188431.html
Copyright © 2011-2022 走看看