将字符串转换为时间并添加一些分钟(Convert string to time and add some minutes)

编程入门 行业动态 更新时间:2024-10-26 00:31:49
将字符串转换为时间并添加一些分钟(Convert string to time and add some minutes)

我有一个表单和一个按钮加上几分钟到一个时间(hh:MM),但时间是一个span标签。

在Firefox上工作得很好,但是当我在Chrome上测试时,不能使用Date()。 发生了什么?

//Botão adicionar horário agenda. $('.button').click(function() { var $duration_schedule = $('#duration'); var duration = $duration_schedule.val(); // 30 var hour = $('.time_schedule_form').text(); // 10:00 var new_time = self.Plus_minutes(hour, duration); alert(new_time); // 10:30 }); Plus_minutes: function(hour, duration) { var time, new_hour, hours = ''; time = new Date("T"+hour); // Erro at Chrome time.setTime(time.getTime() + duration*60000); hours = time.getHours().toString(); minutes = time.getMinutes().toString(); if (minutes.length > 1) { new_hour = hours + ':' + minutes; } else { new_hour = hours + ':0' + minutes; } return new_hour; },

I have a form and one button to plus some minutes to a time(hh:MM), but the time is a span tag.

At Firefox works well, but when I tested at Chrome doesn't work the Date(). What happened?

//Botão adicionar horário agenda. $('.button').click(function() { var $duration_schedule = $('#duration'); var duration = $duration_schedule.val(); // 30 var hour = $('.time_schedule_form').text(); // 10:00 var new_time = self.Plus_minutes(hour, duration); alert(new_time); // 10:30 }); Plus_minutes: function(hour, duration) { var time, new_hour, hours = ''; time = new Date("T"+hour); // Erro at Chrome time.setTime(time.getTime() + duration*60000); hours = time.getHours().toString(); minutes = time.getMinutes().toString(); if (minutes.length > 1) { new_hour = hours + ':' + minutes; } else { new_hour = hours + ':0' + minutes; } return new_hour; },

最满意答案

我想这是因为构造函数的输入而发生的! 如果有时间,你应该在几毫秒内输入数字。 数据对象具有以下构造函数:

新的日期(); 新日期(价值); new Date(dateString); 新日期(年,月[,日期[,小时[,分钟[,秒[,毫秒]]]]]);

您可以查看Developer.mozilla,然后您可以查看有关格式的更好说明。

也许Firefox正在转换为包含该部分代码中的内容。 我找到了有关数据输入格式的其他解释,你也可以看看: 将字符串转换为日期

I suppose that it is happening because of constructor's input! In case of time you should put numbers in milliseconds. The Data Object have those constructors bellow:

new Date(); new Date(value); new Date(dateString); new Date(year, month[, date[, hours[, minutes[, seconds[,milliseconds]]]]]);

you can take at look at Developer.mozilla then you can check a better explanation about formats.

Maybe the Firefox are converting to including something in that part of code. I found out other explanation about Data input formats, you can take a look too at: Convert String to Date

更多推荐

本文发布于:2023-04-29 07:00:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1335259.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:转换为   字符串   时间   Convert   minutes

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!