Click here to Skip to main content
15,902,112 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
my code is

C#
Date.prototype.addMinutes = function (m) {
           this.setHours(this.getHours() + m);
           return this;
       };
Posted
Comments
ZurdoDev 17-Feb-15 8:47am    
Wouldn't you divide m by 60?

1 solution

Hints:
1. The javascript date object has a method .getTime() that allows you to get an integer that represents the number of milliseconds since an arbitrary date*.

2. You can use a number in the same format passed as a parameter when creating/setting a date object.

Note: The arbitrary date is actually "Thu Jan 01 1970 11:00:00 GMT+1100 (AUS Eastern Daylight Time)" over here. - if you use the value of 0 in a call to .setTime you can see this reflected in the resultant date).

With that in mind, consider the following snippet:

JavaScript
var now = new Date();
var hourLater dayLater = new Date();

hourLater dayLater.setTime( now.getTime() + (24 * 60 * 60 * 1000) );


We simply create two date objects and initialize them with the current time.
Next, we get the current time as the number of ms since 1/1/1970 and finally add the number of milliseconds in 24 hours, before using this value to set the second date object.

Finally, punching now and hourLater dayLater into the console gives the following:

> now
< Wed Feb 18 2015 01:09:36 GMT+1100 (AUS Eastern Daylight Time)
> hourLater dayLater
< Thu Feb 19 2015 01:09:36 GMT+1100 (AUS Eastern Daylight Time)


Lastly: Chrome will autosuggest the available members of an object when typing in the console. I.e, if you open it up (Ctrl-Shift-I) and type "var test = new Date();" you'll get a response that says "undefined". If you then type "test.", Chrome will show a drop-down list of available members of the Date object.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900