js格式化时间戳

penven      2022-02-06     167

关键词:

//js格式化时间戳,转换为时间格式  2017-1-15 4:10:15

function getLocalTime(nS) {
    var time = new Date(parseInt(nS) * 1000).toLocaleString();
    var reg =  new RegExp("(/)", "g");
    var reg_time =  new RegExp("(上午|下午)", "g");
    return time.replace(reg,‘-‘).replace(reg_time,‘‘);
}
console.log(getLocalTime(1484467815));

//格式为  2017-01-15 16:10:15

console.log(formatDate(‘1484467815‘));
function  formatDate(obj){
    var time = new Date(parseInt(obj) * 1000);
    var y = time.getFullYear();  //
    var m = time.getMonth() + 1;  //
    if(m < 10){ m = ‘0‘ + m }
    var d = time.getDate();  //
    if(d < 10){ d = ‘0‘ + d }
    var h = time.getHours();  //
    if(h < 10){ h = ‘0‘ + h }
    var mm = time.getMinutes();  //
    if(mm < 10){ mm = ‘0‘ + mm }
    var s = time.getSeconds();  //
    if(s < 10){ s = ‘0‘ + s }
    var timeStr = y+"-"+m+"-"+d+" "+h+":"+mm+":"+s;
    return timeStr;
}

//js格式化当前时间  2017-01-15 16:10:15     2017-01-15 17:12:07    2017-01-15

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;
}
var time1 = new Date(parseInt(1484467815) * 1000).format("yyyy-MM-dd hh:mm:ss");
var time2 = new Date().format("yyyy-MM-dd hh:mm:ss");
var time3 = new Date().format("yyyy-MM-dd");
console.log(time1,time2,time3);
//2017-01-15 16:10:15 2017-01-15 17:12:07 2017-01-15

 

js时间戳格式化日期时间由于mysql数据库里面存储时间存的是时间戳,取出来之后,js要格式化一下显示。

//时间戳转时间functionRiQi(sj){varnow=newDate(sj*1000);varyear=now.getFullYear();varmonth=now.getMonth()+1;vardate=now.getDate();varhour=now.getHours();varminute=now.getMinutes();varsecond=now.getSeconds(); 查看详情

js时间格式处理(时间戳,时间格式,时间转换等)

一、时间转时间戳console.log(Date.parse(newDate()))//结果:1477808630000不推荐这种办法,毫秒级别的数值被转化为000console.log(newDate()).valueOf())//结果:1477808630404通过valueOf()函数返回指定对象的原始值获得准确的时间戳值console.log(newDate().g... 查看详情

js时间戳转换时间格式

functiongetLocalTime(time){ if(time>0){ vardateStr=newDate(time*1000); varstr=""+dateStr.getFullYear()+"-"; varm=dateStr.getMonth(); m=m<10?"0"+m:m; str+=m+"-"; vard=dateStr.getDate(); d 查看详情

Unix时间戳的moment.js格式?

】Unix时间戳的moment.js格式?【英文标题】:moment.jsformatforunixtimestamp?【发布时间】:2016-07-1716:27:10【问题描述】:我正在尝试使用moment.tz将unix时间戳转换为本地时间。我注意到在传递日期时我必须使用格式,就像这样moment.tz(date... 查看详情

js时间戳与日期格式之间的互转

1.将时间戳转换成日期格式//简单的一句代码vardate=newDate(时间戳);//获取一个时间对象注意:如果是uinx时间戳记得乘于1000。比如php函数time()获得的时间戳就要乘于1000/**1.下面是获取时间日期的方法,需要什么样的格式自己拼接起... 查看详情

js把时间戳转换为普通日期格式

第一种functiongetLocalTime(nS){returnnewDate(parseInt(nS)*1000).toLocaleString().replace(/:d{1,2}$/,‘‘);}alert(getLocalTime(1293072805));functiongetLocalTime(nS){returnnewDate(parseInt(nS)*1000).toLocal 查看详情

js时间戳转日期时间(代码片段)

JS日期格式化时间戳转日期时间字符串时间格式化functiontimeFomat(t,e)vars=t?newDate(t):newDate(1e3*t),i=s.getFullYear(),n=s.getMonth()+1,a=s.getDate(),o=s.getHours(),l=s.getMinutes(),u=s.getSe 查看详情

js时间戳转日期时间(代码片段)

JS日期格式化时间戳转日期时间字符串时间格式化functiontimeFomat(t,e)vars=t?newDate(t):newDate(1e3*t),i=s.getFullYear(),n=s.getMonth()+1,a=s.getDate(),o=s.getHours(),l=s.getMinutes(),u=s.getSe 查看详情

js获取到newdate()日期格式化以及获取时间戳

参考技术A项目中经常需要通过newDate()获取时间,但是获取到的时间需要我们个人进行年月日拼接,做法比较麻烦,以下方法绑定在newDate(),可以根据个人需求来输出我们想要的时间。 查看详情

vue.js怎样将时间戳转化为日期格式

...建日期对象。3、然后需要根据图示代码以毫秒为单位的时间戳,设置日期对象。4、然后需要根据图示代码打印设置后的日期。5、保存文件,在浏览器查看输出将时间戳转化为日期格式成功。 查看详情

js时间戳怎么转成日期格式

一.js将时间转换成时间戳1.js获取当前时间戳的方法?123vartimestamp1=Date.parse(newDate());vartimestamp2=(newDate()).valueOf();vartimestamp3=newDate().getTime();第一种:获取的时间戳是把毫秒改成000显示,第二种和第三种是获取了当前毫秒的时间戳。2.j... 查看详情

js内置对象-date-时间戳

...t;当前时间=====构造函数传日期字符串指定具体的日期 2、格式化时间:data.tostring()默认的时间格式,让日期以标准化的日期字符串格式输出 data.toLocaleString()当地的时间格式本地化日期字符串格式输出 3、getFullYear();获取年 get 查看详情

用js将从后台得到的时间戳(毫秒数)转换为想要的日期格式

...者  2016/10/2517:37:30然而我们前台得到的却是一段数字(时间戳,毫秒数):                1477386005   时间戳转化,核心方法:1477386005是从后台得到时间戳(注意:有的时候得到的时间戳是已经... 查看详情

js时间戳怎么转成日期格式

参考技术A使用Date对象可以将毫秒时间戳转为js的Date对象<br>然后再调用Date的getFullYear、getMonth、getDate等方法拼成想要的日期格式<br>vardate=newDate(1433665089755);<br>alert(date.getFullYear()+'/'+(date.getMonth()+1)+'/'+date.g... 查看详情

js时间格式与时间戳的相互转换示例代码

一.时间转换时间戳 functiontransdate(endTime){vardate=newDate();date.setFullYear(endTime.substring(0,4));date.setMonth(endTime.substring(5,7)-1);date.setDate(endTime.substring(8,10));date.setHours(endTime 查看详情

C3.js - 如何在绘制取自 InfluxDB 的时间序列时指定时间戳格式

】C3.js-如何在绘制取自InfluxDB的时间序列时指定时间戳格式【英文标题】:C3.js-HowtospecifythetimestampformatwhenplottingtimeseriestakenfromInfluxDB【发布时间】:2016-01-0123:51:42【问题描述】:influxDB时间戳如下所示:2015-01-29T21:55:43.702900257Z问... 查看详情

移动端js怎么时间格式转换成时间戳

将日期格式转换成时间戳://也很简单date=newDate(\'2014-04-2318:55:49:123\');//传入一个时间格式,如果不传入就是获取现在的时间了,就这么简单。//有三种方式获取time1=date.getTime()time2=date.valueOf()time3=Date.parse(date)//三种获取的区别第一... 查看详情

js时间戳怎么转成日期格式

...-07-09 19:44:01更多自定义参数和用法可以参照:javascript格式化日期时间下面是源码的截图:js时间戳转为日期格式参考技术A时间戳就是从1970/1/10:00:00起到现在的秒数(按格林威治时间算),根据这个就能转换啦。最简单的方法... 查看详情