简体   繁体   English

将时间添加到hh:mm:ss javasctipt的字符串中

[英]Add time to string of hh:mm:ss javasctipt

I have the following string returned by my web service: '10:00:00'. 我的网络服务返回了以下字符串:“ 10:00:00”。

It is successfully being disabled in my timepicker via: 它已通过以下方式在我的时间选择器中成功禁用:

$('#appointmentTimeTextbox').timepicker('option',
                                        'disableTimeRanges',
                                        [[obj.appointmentTime, obj.appointmentEndTime]]);

What I would like is to add 30 minutes (one option in the timepicker) to obj.appointmentEndTime . 我想要将30分钟(时间选择器中的一个选项)添加到obj.appointmentEndTime I have tried: 我努力了:

var appointmentEndTime = new date(obj.appointmentEndTime);
    appointmentEndTime.setHours(appointmentEndTime.getHours() + .5);

to no avail. 无济于事。

You've got a few issues going on here: 您在这里遇到了一些问题:

1) This may just be a typo in the question, but the date constructor must be capitalized: new Date(... 1)这可能只是问题的错字,但日期构造函数必须大写: new Date(...

2) The setHours() method only takes integer values for the "hours" parameter . 2) setHours()方法仅对“ hours”参数采用整数值。 . . from MDN : 来自MDN

hoursValue hoursValue

integer between 0 and 23, representing the hour. 0到23之间的整数,代表小时。

You can add in a half an hour two ways: by providing a "minutes" parameter in the setHours() call: 您可以通过两种方式在半小时内添加:通过在setHours()调用中提供“分钟”参数:

appointmentEndTime.setHours(appointmentEndTime.getHours(), 30);

. . . or by simply using the setMinutes() method: 或仅使用setMinutes()方法即可:

appointmentEndTime.setMinutes(appointmentEndTime.getMinutes() + 30);

3) When you create the new date value with: 3)使用以下方法创建新的日期值时:

var appointmentEndTime = new date(obj.appointmentEndTime);

. . . you are working with a brand new Date object, so you will either need to copy the new time value back into the original obj.appointmentEndTime property, or you will need to create the value from the new appointmentEndTime variable, and pass that into the timepicker() method instead of obj.appointmentEndTime . 你是一个全新的工作Date的对象,所以你要么需要新的时间值复制回原始obj.appointmentEndTime属性,或者你需要创建新的价值appointmentEndTime变量,并传递到timepicker()方法,而不是obj.appointmentEndTime


UPDATE 更新

Okay, so I looked at your Fiddle and the issue that you are having now is that you can't create a date with simply a time value: 好的,所以我查看了您的小提琴,现在遇到的问题是您不能仅使用时间值创建日期:

var appoinementEndTime = new Date('10:00:00');

Again, looking to MDN (a great resource, by the way), the valid inputs for the Date constructor are: 再次,使用MDN (顺便说一句,这是一个很好的资源), Date构造函数的有效输入为:

new Date();           // The exact moment at the creation of the Date
new Date(value);      // In milliseconds, since 1 January 1970 00:00:00 UTC
new Date(dateString); // A string value representing a date (see link for valid formats)
new Date(year, month[, date[, hours[, minutes[, seconds[, milliseconds]]]]]);

So your best option would be to pick a date (the current date would be easiest), and then override the time values on that date, to get the value that you want: 因此,最好的选择是选择一个日期(当前日期将是最简单的),然后覆盖该日期的时间值,以获得所需的值:

var endTimeArray = obj.appoinementEndTime.split(":");
var appoinementEndTime = new Date();
appoinementEndTime.setHours(endTimeArray[0],      // the hours value
                            endTimeArray[1] + 30, // the minutes value
                            endTimeArray[2],      // the seconds value
                            0);                   // the milliseconds value

Using that (and your value of '10:00:00 ), the time value of the date should be 10:30:00 (which you can create using the getHours() , getMinutes() , and getSeconds methods on appoinementEndTime`). 使用该值(以及您的值'10:00:00 ), the time value of the date should be 10:30:00 (which you can create using the appoinementEndTime` (which you can create using the getHours() , getMinutes() , and getSeconds methods on创建日期) 。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM