时间戳js转换

模糊的星空      2022-02-06     447

关键词:

// 获取当前时间戳(以s为单位)
var timestamp = Date.parse(new Date());
timestamp = timestamp / 1000;
//当前时间戳为:1403149534
console.log("当前时间戳为:" + timestamp);

// 获取某个时间格式的时间戳
var stringTime = "2014-07-10 10:21:12";
var timestamp2 = Date.parse(new Date(stringTime));
timestamp2 = timestamp2 / 1000;
//2014-07-10 10:21:12的时间戳为:1404958872 
console.log(stringTime + "的时间戳为:" + timestamp2);

// 将当前时间换成时间格式字符串
var timestamp3 = 1403058804;
var newDate = new Date();
newDate.setTime(timestamp3 * 1000);
// Wed Jun 18 2014 
console.log(newDate.toDateString());
// Wed, 18 Jun 2014 02:33:24 GMT 
console.log(newDate.toGMTString());
// 2014-06-18T02:33:24.000Z
console.log(newDate.toISOString());
// 2014-06-18T02:33:24.000Z 
console.log(newDate.toJSON());
// 2014年6月18日 
console.log(newDate.toLocaleDateString());
// 2014年6月18日 上午10:33:24 
console.log(newDate.toLocaleString());
// 上午10:33:24 
console.log(newDate.toLocaleTimeString());
// Wed Jun 18 2014 10:33:24 GMT+0800 (中国标准时间)
console.log(newDate.toString());
// 10:33:24 GMT+0800 (中国标准时间) 
console.log(newDate.toTimeString());
// Wed, 18 Jun 2014 02:33:24 GMT
console.log(newDate.toUTCString());

Date.prototype.format = function(format) {
       var date = {
              "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+)/i.test(format)) {
              format = format.replace(RegExp.$1, (this.getFullYear() + ‘‘).substr(4 - RegExp.$1.length));
       }
       for (var k in date) {
              if (new RegExp("(" + k + ")").test(format)) {
                     format = format.replace(RegExp.$1, RegExp.$1.length == 1
                            ? date[k] : ("00" + date[k]).substr(("" + date[k]).length));
              }
       }
       return format;
}
console.log(newDate.format(‘yyyy-MM-dd h:m:s‘));

</script>
技术分享

js时间与时间戳的转换

 一:时间转时间戳:javascript获得时间戳的方法有四种,都是通过实例化时间对象 newDate()来进一步获取当前的时间戳1.vartimestamp1=Date.parse(newDate());//结果:1477808630000不推荐这种办法,毫秒级别的数值被转化为000 console.l... 查看详情

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 查看详情

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

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

原生js时间戳获取和转换

时间转变为时间戳的方法汇总constdate=newDate('2021-4-1208:22:22');console.log(date);//MonApr12202108:22:22GMT+0800(中国标准时间)console.log(date*1);//1618186942000console.log(Number(date));//1618186942000console.log(da 查看详情

前端js转换时间戳为时间类型显示

1//事件戳转换2functionadd0(m){returnm<10?‘0‘+m:m}3functionformatDate(timestamp)4{5//timestamp是整数,否则要parseInt转换6vartime=newDate(timestamp);7vary=time.getFullYear();8varm=time.getMonth()+1;9vard=time.getD 查看详情

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

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

时间戳转换成时间js(年-月-日,例如“2017-04-22”)

1functionGetDateByShiJianChuo(timespan){2vardate=newDate(parseInt(timespan.replace("/Date(","").replace(")/","")));3vardateStr=date.toLocaleDateString();4dateStr=dateStr.replace("/","-").replace("/"," 查看详情

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 查看详情

js怎么把时间戳转换成几分钟几小时前

将时间戳转换成日期格式://简单的一句代码vardate=newDate(时间戳);//获取一个时间对象注意:如果是uinx时间戳记得乘于1000。比如php函数time()获得的时间戳就要乘于1000/*----------下面是获取时间日期的方法参考技术A使用Date函数如:dn... 查看详情

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

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

如何将 Firestore 日期/时间戳转换为 JS Date()?

】如何将Firestore日期/时间戳转换为JSDate()?【英文标题】:HowdoIconvertaFirestoredate/TimestamptoaJSDate()?【发布时间】:2019-02-1406:39:37【问题描述】:我正在尝试将以下日期转换为javascriptDate()对象。当我从服务器取回它时,它是一个Time... 查看详情

jssqlc#时间时间戳相互转换

js、sql、C#时间、时间戳相互转换//1.获取当前时间戳_c#longaa=(DateTime.Now.ToUniversalTime().Ticks-621355968000000000)/10000000//2.时间戳->时间C#DateTimeb11=GetTime("1483461862");//11位时间戳->时间DateTimeb13=ConvertStringToDa 查看详情

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

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

js怎么把时间戳转换为日期格式

参考技术Ajs怎么把时间戳转换为日期格式前端有时候可能要从日期控件中拿到日期,然后参与计算,下边记录一个把日期字符串转换成时间戳的小函数。dateStr格式为“2014-05-0800:22:11”functionget_unix_time(dateStr)varnewstr=dateStr.replace(/-/... 查看详情

使用 moment.js 将跨越秋季 DST 边界的一系列非 UTC 时间戳转换为 UTC 时间戳的正确方法?

】使用moment.js将跨越秋季DST边界的一系列非UTC时间戳转换为UTC时间戳的正确方法?【英文标题】:Correctwaytousemoment.jstoconvertaseriesofnon-UTCtimestampsthatcrosstheFallDSTboundarytoUTCtimestamps?【发布时间】:2017-09-0716:29:09【问题描述】:背景我... 查看详情

js把时间戳只转换为“时“和”分”

比如把这个时间戳:1491386842(它是2017-4-518:07:22)转换为:18:07一行js代码实现时间戳转时间格式javascript时间戳转日期时间,支持自定义输出日期格式,可以显示年,月,周,日,时,分,秒多种形式的日期和时间。var date = for... 查看详情

js将时间戳转换为几月几日星期几

...n"20"+year+"."+month+"."+date;,扩展资料js时间转换(时间戳转date,年月日时分秒)使用:1.formatDateTime("时间戳","yyyy-MM-dd")2.formatDateTime("时间戳","yyyyMMdd")exportfunctionformatDateTime(time,forma... 查看详情

js格式化时间戳

//js格式化时间戳,转换为时间格式 2017-1-154:10:15functiongetLocalTime(nS){vartime=newDate(parseInt(nS)*1000).toLocaleString();varreg=newRegExp("(/)","g");varreg_time=newRegExp("(上午|下午)","g");returntime.replace 查看详情