首页 新闻 会员 周边

Javascript代码的小小疑问(18)

0
悬赏园豆:5 [已解决问题] 解决于 2015-06-12 08:52

一.先看以下一段js代码:(放在chrome的console里)

  1. function CurentTime()

    var now = new Date();

    var year = now.getFullYear();         //年
    var month = now.getMonth() + 1;  //月
    var day = now.getDate();              //日

    var hh = now.getHours();              //时
    var mm = now.getMinutes();         //分
    var ss = now.getSeconds();           //秒

    var clock = year + "-";

    if(month < 10)
    clock += "0";

    clock += month + "-";

    if(day < 10)
    clock += "0";

    clock += day + " ";

    if(hh < 10)
    clock += "0";

    clock += hh + ":";
    if (mm < 10) clock += '0'; 
    clock += mm + ":"; 

    if (ss < 10) clock += '0'; 
    clock += ss; 
    return(clock); 
    };

alert(+new Date(CurentTime()));

 

二. 疑问:为何不是系统当前时间?

问题补充:

博客园的大神都在忙吗还是不屑这个问题呢?

 

但是我用alert(CurentTime());  //正常

        用alert(+new Date(CurentTime()));  //时间戳是时间戳,但不是当前时间戳,为什么?

Coca-code的主页 Coca-code | 初学一级 | 园豆:10
提问于:2015-06-10 13:14
< >
分享
最佳答案
0

你指的时间戳是什么格式的?你在拼接时间的时候,把毫秒这一级别的数据忽略了。时间处理,建议直接使用moment库。

收获园豆:5
幻天芒 | 高人七级 |园豆:37175 | 2015-06-11 14:44

有毫秒吗〉我只用getSeconds();啊,

不用那个库就不能实现了吗?

Coca-code | 园豆:10 (初学一级) | 2015-06-11 14:50

没办法

Coca-code | 园豆:10 (初学一级) | 2015-06-11 14:50

@殷敏峰: getMilliseconds(),毫秒。不用库当然可以实现,就是要考虑周全。

幻天芒 | 园豆:37175 (高人七级) | 2015-06-11 15:15

@幻天芒: 那是肯定的。

Coca-code | 园豆:10 (初学一级) | 2015-06-11 15:20

@殷敏峰: :)

幻天芒 | 园豆:37175 (高人七级) | 2015-06-11 15:22

@幻天芒: 关键现在还是出不来

Coca-code | 园豆:10 (初学一级) | 2015-06-11 15:22

@殷敏峰: 你想要什么样子的时间戳,其实我不知道你想要什么~

幻天芒 | 园豆:37175 (高人七级) | 2015-06-11 15:24

@幻天芒: 是吗?简单的,如1506111637     15年6月11号16时37分,有点难度。

Coca-code | 园豆:10 (初学一级) | 2015-06-11 16:37

@殷敏峰: 如1506111637     15年6月11号16时37分这哪里是时间戳

liaoshifa10 | 园豆:268 (菜鸟二级) | 2015-06-11 17:44

@殷敏峰: 不要拿这些数据去作为new Date的参数,它不认识。写个函数返回指定格式的文本,就是你需要的。

幻天芒 | 园豆:37175 (高人七级) | 2015-06-11 19:51
其他回答(2)
0
(new Date).valueOf()
1433929153065
(new Date).getTime()
1433929181348
_Ong | 园豆:202 (菜鸟二级) | 2015-06-10 17:37
0

alert(CurentTime());再alert(+new Date(CurentTime())); 这两个的时间是不一样的,因为CurentTime()执行了两次,而两次执行是有间隔的,如果想两个时间一致就赋值给一个变量var time=CurentTime(); alert(time);alert(+new Date(time))

liaoshifa10 | 园豆:268 (菜鸟二级) | 2015-06-10 17:40

function CurentTime()
{
var now = new Date();

var year = now.getFullYear(); //年
var month = now.getMonth() + 1; //月
var day = now.getDate(); //日

var hh = now.getHours(); //时
var mm = now.getMinutes(); //分
var ss = now.getSeconds(); //秒

var clock = year + "-";

if(month < 10)
clock += "0";

clock += month + "-";

if(day < 10)
clock += "0";

clock += day + " ";

if(hh < 10)
clock += "0";

clock += hh + ":";
if (mm < 10) clock += '0';
clock += mm + ":";

if (ss < 10) clock += '0';
clock += ss;
return(clock);
}; var myTime = CurentTime();
alert(+new Date(myTime));  //还是无法获取当前系统的时间戳

 

支持(0) 反对(0) Coca-code | 园豆:10 (初学一级) | 2015-06-11 11:00

@殷敏峰: 当前时间是一直在变的,肯定存在误差,alert(+new Date(myTime));只是弹出执行的时候那个时间的时间戳

支持(0) 反对(0) liaoshifa10 | 园豆:268 (菜鸟二级) | 2015-06-11 12:23

@liaoshifa10: 误差是有,但误差太大了,即便一直不动。

支持(0) 反对(0) Coca-code | 园豆:10 (初学一级) | 2015-06-11 14:48

@殷敏峰: 1秒就相差1000了,不知道你想干嘛

支持(0) 反对(0) liaoshifa10 | 园豆:268 (菜鸟二级) | 2015-06-11 15:38

@liaoshifa10: 那你试试能出结果吗?

支持(0) 反对(0) Coca-code | 园豆:10 (初学一级) | 2015-06-11 16:35
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册