最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - Adding one minute to current time in Javascript - Stack Overflow

programmeradmin2浏览0评论

I use Date object to create time in my Javascript code and it should be formated like so : 08:04:21. This is how I tried to do it:

$('#time').click(function(){
                var currentTime = new Date();
                var Time=currentTime.getHours() + ":"
                + currentTime.getMinutes() + ":"
                + currentTime.setSeconds(currentTime.getSeconds() + 60);
                console.log(Time);
                $(this).val(Time);
            });

But when Time is logged in console string looks like this 8:1:1467844916075. Same happens when i try this:

var Time=currentTime.getHours() + ":"
                    + currentTime.setMinutes(currentTime.getMinutes() + 1) + ":"
                    + currentTime.getSeconds();

It bring out similar result : 8:1467844916075:3. I even tried this answer: javascript add one minute to time object

$('#time').click(function(){
                var currentTime = new Date();
                var Time = currentTime.setTime(currentTime.getTime() + 1000 * 60);
                console.log(Time);
                $(this).val(Time);
            });

But Time in this case looks like this: 1467785566719. Any idea how to get human readable current time(not date) plus one minute?

I use Date object to create time in my Javascript code and it should be formated like so : 08:04:21. This is how I tried to do it:

$('#time').click(function(){
                var currentTime = new Date();
                var Time=currentTime.getHours() + ":"
                + currentTime.getMinutes() + ":"
                + currentTime.setSeconds(currentTime.getSeconds() + 60);
                console.log(Time);
                $(this).val(Time);
            });

But when Time is logged in console string looks like this 8:1:1467844916075. Same happens when i try this:

var Time=currentTime.getHours() + ":"
                    + currentTime.setMinutes(currentTime.getMinutes() + 1) + ":"
                    + currentTime.getSeconds();

It bring out similar result : 8:1467844916075:3. I even tried this answer: javascript add one minute to time object

$('#time').click(function(){
                var currentTime = new Date();
                var Time = currentTime.setTime(currentTime.getTime() + 1000 * 60);
                console.log(Time);
                $(this).val(Time);
            });

But Time in this case looks like this: 1467785566719. Any idea how to get human readable current time(not date) plus one minute?

Share Improve this question edited May 23, 2017 at 10:30 CommunityBot 11 silver badge asked Jul 6, 2016 at 6:15 Ognj3nOgnj3n 7598 silver badges30 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 2

Please try this,

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);

You can check this:

Date.getTime() returns you the number of milliseconds since 1970/01/01. So just grab it and add 1 minute to it to form new milliseconds count for new date.

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());

Here is working example https://jsfiddle/oa7j3krs/2/

$('#time').click(function(){
    var d = new Date($.now()+60*1000); // current time + 60s
    $(this).val(d.getHours()+':'+d.getMinutes()+':'+d.getSeconds());
});

Anyway, I found a way to do this. Maybe it's not the best but it will do the job.

$('#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);
            });
发布评论

评论列表(0)

  1. 暂无评论