I am trying to subtract hours from a given date time string using javascript. My code is like:
var cbTime = new Date();
cbTime = selectedTime.setHours(-5.5);
Where selectedTime
is the given time (time that i pass as parameter).
So suppose selectedTime
is Tue Sep 16 19:15:16 UTC+0530 2014
Ans I get is : 1410875116995
I want answer in datetime format. Am I doing something wrong here? Or there is some other solution?
The setHours() method sets the hours for a specified date according to local time, and returns the number of milliseconds since January 1, 1970 00:00:00 UTC until the time represented by the updated Date instance.
JavaScript Date objects represent a single moment in time in a platform-independent format. Date objects contain a Number that represents milliseconds since 1 January 1970 UTC.
Set hours of work means an employer controls when work is performed. An independent contractor has more freedom as to when the work is completed. Sample 1.
The reason is that setHours()
, setMinutes()
, etc, take an Integer
as a parameter. From the docs:
...
The setMinutes()
method sets the minutes for a specified date according to local time....
Parameters:
An integer between 0 and 59, representing the minutes.
So, you could do this:
var selectedTime = new Date(),
cbTime = new Date();
cbTime.setHours(selectedTime.getHours() - 5);
cbTime.setMinutes(selectedTime.getMinutes() - 30);
document.write('cbTime: ' + cbTime);
document.write('<br>');
document.write('selectedTime: ' + selectedTime);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With