我想要一个表示当前日期和时间的数字,比如Unix时间戳。
当前回答
下面是一个生成时间戳的简单函数,格式为:mm/dd/yy hh:mi:ss
function getTimeStamp() {
var now = new Date();
return ((now.getMonth() + 1) + '/' +
(now.getDate()) + '/' +
now.getFullYear() + " " +
now.getHours() + ':' +
((now.getMinutes() < 10)
? ("0" + now.getMinutes())
: (now.getMinutes())) + ':' +
((now.getSeconds() < 10)
? ("0" + now.getSeconds())
: (now.getSeconds())));
}
其他回答
这似乎奏效了。
console.log(clock.now);
// returns 1444356078076
console.log(clock.format(clock.now));
//returns 10/8/2015 21:02:16
console.log(clock.format(clock.now + clock.add(10, 'minutes')));
//returns 10/8/2015 21:08:18
var clock = {
now:Date.now(),
add:function (qty, units) {
switch(units.toLowerCase()) {
case 'weeks' : val = qty * 1000 * 60 * 60 * 24 * 7; break;
case 'days' : val = qty * 1000 * 60 * 60 * 24; break;
case 'hours' : val = qty * 1000 * 60 * 60; break;
case 'minutes' : val = qty * 1000 * 60; break;
case 'seconds' : val = qty * 1000; break;
default : val = undefined; break;
}
return val;
},
format:function (timestamp){
var date = new Date(timestamp);
var year = date.getFullYear();
var month = date.getMonth() + 1;
var day = date.getDate();
var hours = date.getHours();
var minutes = "0" + date.getMinutes();
var seconds = "0" + date.getSeconds();
// Will display time in xx/xx/xxxx 00:00:00 format
return formattedTime = month + '/' +
day + '/' +
year + ' ' +
hours + ':' +
minutes.substr(-2) +
':' + seconds.substr(-2);
}
};
表演
今天-2020.04.23我对选定的解决方案进行测试。我在Chrome 81.0、Safari 13.1和Firefox 75.0上测试了MacOs High Sierra 10.13.6
结论
Solution Date.now()(E)在Chrome和Safari上最快,在Firefox上第二快,这可能是快速跨浏览器解决方案的最佳选择解决方案性能.now()(G),令人惊讶的是,它比Firefox上的其他解决方案快100多倍,但在Chrome上最慢解决方案C、D、F在所有浏览器上都很慢
细节
铬的结果
您可以在此处对机器进行测试
测试中使用的代码显示在下面的代码段中
函数A(){return new Date().getTime();}函数B(){return new Date().valueOf();}函数C(){return+new Date();}函数D(){返回新日期()*1;}函数E(){return Date.now();}函数F(){return Number(new Date());}函数G(){//此解决方案返回从加载页面开始计算的时间。//(在Chrome上,它提供了更好的精度)return performance.now();}//测试log=(n,f)=>console.log(`${n}:${f()}`);日志('A',A);日志('B',B);日志('C',C);日志('D',D);对数('E',E);日志('F',F);日志('G',G);此代码段仅显示外部基准测试中使用的代码
我必须创建一个TIMESTAMP,尽管我的DB映射上的类型是String,为此我使用了
new Date().toISOString();
输出类似于“2023-01-09T14:11:31.931Z”
我喜欢这个,因为它很小:
+new Date
我也喜欢这个,因为它同样短,而且与现代浏览器兼容,超过500人投票认为它更好:
Date.now()
//当前Unix时间戳//自1970年1月1日起,1443534720秒。(UTC)//秒console.log(数学地板(newDate().valueOf()/1000));//1443534720console.log(数学地板(Date.now()/1000));//1443534720console.log(数学地板(newDate().getTime()/1000));//1443534720//毫秒console.log(数学地板(newDate().valueOf()));//1443534720087console.log(数学地板(Date.now()));//1443534720087console.log(数学地板(newDate().getTime()));//1443534720087//jQuery//秒console.log(数学地板($.now()/1000));//1443534720//毫秒console.log($.now());//1443534720087<script src=“https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js“></script>
推荐文章
- ReactJS和公共文件夹中的图像
- 在React Native中使用Fetch授权头
- 为什么我的球(物体)没有缩小/消失?
- 如何使用jQuery检测页面的滚动位置
- if(key in object)或者if(object. hasownproperty (key)
- 一元加/数字(x)和parseFloat(x)之间的区别是什么?
- angularjs中的compile函数和link函数有什么区别
- 删除绑定中添加的事件监听器
- 很好的初学者教程socket.io?
- 在Android应用程序中显示当前时间和日期
- 字符串不能识别为有效的日期时间“格式dd/MM/yyyy”
- HtmlSpecialChars在JavaScript中等价于什么?
- 如何转换日期时间?将日期时间
- React: 'Redirect'没有从' React -router-dom'中导出
- 如何在React中使用钩子强制组件重新渲染?