关键词:
function timestampFormat( timestamp )
function zeroize( num )
return (String(num).length == 1 ? ‘0‘ : ‘‘) + num;
var curTimestamp = parseInt(new Date().getTime() / 1000); //当前时间戳
var timestampDiff = curTimestamp - timestamp; // 参数时间戳与当前时间戳相差秒数
var curDate = new Date( curTimestamp * 1000 ); // 当前时间日期对象
var tmDate = new Date( timestamp * 1000 ); // 参数时间戳转换成的日期对象
var Y = tmDate.getFullYear(), m = tmDate.getMonth() + 1, d = tmDate.getDate();
var H = tmDate.getHours(), i = tmDate.getMinutes(), s = tmDate.getSeconds();
if ( timestampDiff < 60 ) // 一分钟以内
return "刚刚";
else if( timestampDiff < 3600 ) // 一小时前之内
return Math.floor( timestampDiff / 60 ) + "分钟前";
else if ( curDate.getFullYear() == Y && curDate.getMonth()+1 == m && curDate.getDate() == d )
return ‘今天‘ + zeroize(H) + ‘:‘ + zeroize(i);
else
var newDate = new Date( (curTimestamp - 86400) * 1000 ); // 参数中的时间戳加一天转换成的日期对象
if ( newDate.getFullYear() == Y && newDate.getMonth()+1 == m && newDate.getDate() == d )
return ‘昨天‘ + zeroize(H) + ‘:‘ + zeroize(i);
else if ( curDate.getFullYear() == Y )
return zeroize(m) + ‘月‘ + zeroize(d) + ‘日 ‘ + zeroize(H) + ‘:‘ + zeroize(i);
else
return Y + ‘年‘ + zeroize(m) + ‘月‘ + zeroize(d) + ‘日 ‘ + zeroize(H) + ‘:‘ + zeroize(i);
调用
timestampFormat(1326170770); //2012年01月10日 12:46
timestampFormat(Date.parse(‘2016-10-11 15:26:10‘)/1000); //刚刚
timestampFormat(Date.parse(‘2016-10-11 15:10:10‘)/1000); //16分钟前
timestampFormat(Date.parse(‘2016-10-11 10:10:10‘)/1000); //今天10:10
timestampFormat(Date.parse(‘2016-10-10 10:10:10‘)/1000); //昨天10:10
timestampFormat(Date.parse(‘2016-02-10 10:10:10‘)/1000); //02月10日 10:10
timestampFormat(Date.parse(‘2012-10-10 10:10:10‘)/1000); //2012年10月10日 10:10
js时间格式转换
functiontimestampFormat(timestamp)functionzeroize(num)return(String(num).length==1?‘0‘:‘‘)+num;varcurTimestamp=parseInt(newDate().getTime()/1000);//当前时间戳vartimestampDiff=curTimestamp-timestamp;//参数 查看详情
js把时间戳转换为普通日期格式
第一种functiongetLocalTime(nS){returnnewDate(parseInt(nS)*1000).toLocaleString().replace(/:d{1,2}$/,‘‘);}alert(getLocalTime(1293072805));functiongetLocalTime(nS){returnnewDate(parseInt(nS)*1000).toLocal 查看详情
js日期时间格式化转换方法
Date.prototype.format=function(fmt){varo={"M+":this.getMonth()+1,//月份"d+":this.getDate(),//日"h+":this.getHours(),//小时"m+":this.getMinutes(),//分"s+":this.getSeconds(),//秒"q+":Math.floor((this.getMonth( 查看详情
节点js中的日期格式转换
...期格式转换【英文标题】:Dateformatconversioninnodejs【发布时间】:2015-11-1923:07:23【问题描述】:我正在使用一个表单,用户可以从该表单中以dd-mm-yyyyhh:mm:ss格式从日期时间选择器中选择日期时间。现在我想将格式转换为yyyy-mm-ddhh:m... 查看详情
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怎么时间格式转换成时间戳
将日期格式转换成时间戳://也很简单date=newDate(\'2014-04-2318:55:49:123\');//传入一个时间格式,如果不传入就是获取现在的时间了,就这么简单。//有三种方式获取time1=date.getTime()time2=date.valueOf()time3=Date.parse(date)//三种获取的区别第一... 查看详情
js时间转换,能够把时间转换成yyyymmdd格式或yyyymm格式
...义转换函数function dateConvert(dateParms) // 对传入的时间参数进行判断 if(dateParms instanceof Date) var datetime=dateParms; //判断是否为字符串 if((typeof dateParms=="string")&&dateParms.constructor==String) ... 查看详情
js怎么把时间戳转换为日期格式
参考技术Ajs怎么把时间戳转换为日期格式前端有时候可能要从日期控件中拿到日期,然后参与计算,下边记录一个把日期字符串转换成时间戳的小函数。dateStr格式为“2014-05-0800:22:11”functionget_unix_time(dateStr)varnewstr=dateStr.replace(/-/... 查看详情
js转换date日期格式
...需要的,Date()返回格式:ThuMar19201512:00:00GMT+0800(中国标准时间)而我们则需要这样的格式:2015-3-1912:00:00除非是在后台处理好时间格式,然后在页面直接显示。那如何用js格式化date日期值呢?1.js方法返回值:2015-03-19varformatDate=functi 查看详情
java转换json日期/date(1487053489965+0800)/格式以及js时间格式tuefeb14201714:06:32gmt+0800
/Date(1487053489965+0800)/用Java怎么转换成yyyy-MM-dd的格式TueFeb14201714:06:32GMT+0800用Java怎么转换成yyyy-MM-dd的格式跟普通的日期不太一样publicstaticvoidmain(String[]args){Stringstr="/Date(1487053489965+0800)/";str=str.replace( 查看详情
用js将从后台得到的时间戳(毫秒数)转换为想要的日期格式
...者 2016/10/2517:37:30然而我们前台得到的却是一段数字(时间戳,毫秒数): 1477386005 时间戳转化,核心方法:1477386005是从后台得到时间戳(注意:有的时候得到的时间戳是已经... 查看详情
js时间类型的自定义转换库datetime-fmt
在前端开发中,因为js自带的时间格式不是很理想,在操作时间类型的时候总是遇到时间格式的问题,比如后端返回了一个不理想的时间格式,或者你想显示一个更好的时间格式,只能自己又花时间造轮子。除了momentjs有没有更... 查看详情
在js中如何把毫秒转换成规定的日期时间格式
最近做的一个项目,从后台查询到日期数据传到前台JS中遍历之后显示的是以"14"开头的毫秒数,刚开始想用"simpleDateFormat"类处理,结果显示都不显示了。后来查了一下资料,感觉其实很简单,下面把自己的方法分享给大家以供参... 查看详情
在 JavaScript/Node.js 中将 Youtube Data API V3 视频持续时间格式转换为秒
】在JavaScript/Node.js中将YoutubeDataAPIV3视频持续时间格式转换为秒【英文标题】:ConvertingYoutubeDataAPIV3videodurationformattosecondsinJavaScript/Node.js【发布时间】:2014-04-0415:02:01【问题描述】:我正在尝试将ISO8601字符串转换为JS/Node中的秒数... 查看详情
html创建bootstrapdatepicker并使用moment将时间格式从c#转换为js可读(代码片段)
时间格式转换—将后台返回的/date(1448954018000)/格式转换为正常的时间格式
用JS实现方法:functionChangeDateFormat(cellval){vardate=newDate(parseInt(cellval.replace("/Date(","").replace(")/",""),10));varmonth=date.getMonth()+1<10?"0"+(date.getMonth()+1):date.getMonth()+1;varcurr 查看详情
js格式化时间戳
//js格式化时间戳,转换为时间格式 2017-1-154:10:15functiongetLocalTime(nS){vartime=newDate(parseInt(nS)*1000).toLocaleString();varreg=newRegExp("(/)","g");varreg_time=newRegExp("(上午|下午)","g");returntime.replace 查看详情
在 JS 中将每秒位数 (BPS) 转换为可读的大小格式
...文标题】:Convertbitspersecond(BPS)toareadablesizeformatinJS【发布时间】:2017-10-1111:23:04【问题描述】:我有以下图表,显示随时间变化的每秒位数:我想重新格式化大小格式,使其看起来更具可读性。为此,我必须确定要显示的正确尺... 查看详情