最近从Android开发跑去做小程序项目了,接收到的数据里面有一个时间戳,怎么把时间戳转化为具体的时间了,于是百度找到了如下代码,清晰易懂。
formatDate:function(date) {
var now = new Date(date)
var year= now.getYear();
var month= now.getMonth() + 1;
var date= now.getDate();
var hour= now.getHours();
var minute= now.getMinutes();
var second= now.getSeconds();
return "20"+ year + "." + month + "." + date;
},
得到的结果是这样的:
2017年变成了20117年。
进行修改:
formatDate:function(date) {
var year = (now.getYear() < 1900) ? (1900 + now.getYear()) : now.getYear() var month= now.getMonth() + 1; var date= now.getDate(); var hour= now.getHours(); var minute= now.getMinutes(); var second= now.getSeconds(); return year + "." + month + "." + date; },var now = new Date(date)
ps:其中date为时间戳