Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

date.setHours() not working

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?

like image 265
user1181942 Avatar asked Sep 17 '14 09:09

user1181942


People also ask

What does date setHours do?

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.

What is date () JavaScript?

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.

What are set hours?

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.


1 Answers

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);
like image 110
toesslab Avatar answered Sep 29 '22 12:09

toesslab