在Javascript中为当前时间添加一分钟
Adding one minute to current time in Javascript
我使用Date
对象在Javascript
代码中创建时间,它的格式应该是这样的:08:04:21
。这就是我试图做到的:
$('#time').click(function(){
var currentTime = new Date();
var Time=currentTime.getHours() + ":"
+ currentTime.getMinutes() + ":"
+ currentTime.setSeconds(currentTime.getSeconds() + 60);
console.log(Time);
$(this).val(Time);
});
但当Time
登录控制台时,字符串看起来像这个8:1:1467844916075
。当我尝试这个时也会发生同样的情况:
var Time=currentTime.getHours() + ":"
+ currentTime.setMinutes(currentTime.getMinutes() + 1) + ":"
+ currentTime.getSeconds();
得出了相似的结果:8:1467844916075:3。我甚至尝试过这个答案:javascript向时间对象添加一分钟
$('#time').click(function(){
var currentTime = new Date();
var Time = currentTime.setTime(currentTime.getTime() + 1000 * 60);
console.log(Time);
$(this).val(Time);
});
但在这种情况下,时间看起来是这样的:1467785566719
。知道如何获得人类可读的当前时间(而不是日期(加一分钟吗?
请尝试这个,
var currentTime = new Date();
var Time = currentTime.setTime(currentTime.getTime() + 1000 * 60);
console.log(Time);
var date = new Date(Time);
// Hours part from the timestamp
var hours = date.getHours();
// Minutes part from the timestamp
var minutes = "0" + date.getMinutes();
// Seconds part from the timestamp
var seconds = "0" + date.getSeconds();
// Will display time in 10:30:23 format
var formattedTime = hours + ':' + minutes.substr(-2) + ':' + seconds.substr(-2);
console.log(formattedTime);
您可以检查以下内容:
Date.getTime()
返回自1970/01/01以来的毫秒数。所以,只要抓住它,再加上1分钟,就可以为新日期形成新的毫秒计数。
var d = new Date();
var millisecondssince1970 = d.getTime();
var newMillisec = millisecondssince1970 + (1000 * 60);
var newDate = new Date(newMillisec);
console.log(newDate.getHours() + ":"
+ newDate.getMinutes() + ":"
+ newDate.getSeconds());
下面是一个工作示例https://jsfiddle.net/oa7j3krs/2/
$('#time').click(function(){
var d = new Date($.now()+60*1000); // current time + 60s
$(this).val(d.getHours()+':'+d.getMinutes()+':'+d.getSeconds());
});
无论如何,我找到了一种方法。也许它不是最好的,但它能胜任工作。
$('#time').click(function(){
var currentTime = new Date();
var addOneMinute = currentTime.getMinutes();
addOneMinute=parseInt(addOneMinute) + 1;
var Time=currentTime.getHours() + ":"
+ addOneMinute + ":"
+ currentTime.getSeconds();
console.log(Time);
$(this).val(Time);
});
相关文章:
- Java脚本时间添加
- 将当前时间添加到我的页面上的特定部分
- 微笑时间轴小部件添加自定义字段
- 谷歌图表API:添加空白行到时间线
- 将时间缩放添加到折线图(Chart.js)
- 如何在此函数中添加处理时间
- 将时间添加到日期值
- 如何在非活动时间后添加随机输入
- 使用JavaScript将日期/时间添加到我的asp.net用户控件中
- Javascript html5 视频在特定时间添加/删除类
- Jquery 将时间添加到日期字段
- 将Google日历事件结束时间添加到脚本中
- 将事件时间添加到一天中-完整日历
- 将持续时间添加到某个时刻(moment.js)
- 在计时器运行后,将持续时间添加到JS setTimeout
- 如何使用JQuery将时间添加到日期实例中,并通过减去两个日期来获得小时
- javascript将可变时间添加到日期和时间中
- 将时间添加到javascript时钟
- Unix 纪元时间:添加天数
- 在Javascript中为当前时间添加一分钟