Click here to Skip to main content
15,886,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Session contain time(min).Either positive or Negative.I want to add that time in current date.If positive,it should get added.If negative,it should get reduced from that.I want to do it from javascript.

1 :
C#
var Time = '<%=Session["Time"]%>';
        var d1 = new Date(),
        d2 = new Date(d1);
        d2.setMinutes(d1.getMinutes() + Time);
        alert(d2);

When I am writing like this (above code) I am getting invalid date.

C#
var Time = '<%=Session["Time"]%>';
         var d1 = new Date(),
         d2 = new Date(d1);
         d2.setMinutes(d1.getMinutes() + 30);
         alert(d2);

When I am writing like this (above code) I am getting proper date.Is it a right way to add min to current date.

2 : I am selecting date from date picker,that gives me date like this '03/30/2016',I have another dropdownlist for selecting hours and min. I want to add hours and time in selected date.Hours is in 24.Time in 60.

C#
var d = new Date('03/30/2016');
        d.setHours(d.getHours() + 17);
        d.setMinutes(d.getMinutes() + 54);
        alert(d);

Is it a right way to do?

3 : I want compare both date d and d2. d should be greater than d2.If not,should show message to user.

What I have tried:

C#
var Time = '<%=Session["Time"]%>';
         var d1 = new Date(),
         d2 = new Date(d1);
         d2.setMinutes(d1.getMinutes() + Time);
         alert(d2);

When I am writing like this (above code) I am getting invalid date.

C#
var Time = '<%=Session["Time"]%>';
         var d1 = new Date(),
         d2 = new Date(d1);
         d2.setMinutes(d1.getMinutes() + 30);
         alert(d2);
Posted
Updated 26-May-21 20:42pm
v3

Here are small snippets you can club them to accomplish your task

- Add minutes in date in javascript
JavaScript
function addMinutes(date, minutes)
{
    return new Date(date.getTime() + minutes*60000);
}
//OR
addMinutes(new Date(2014,10,2), 60*24)

- Subtract minutes from date
JavaScript
var endDate = somedate;
var startdate = new Date(endDate);
var durationInMinutes = 20;
startdate.setMinutes(endDate.getMinutes() - durationInMinutes);

- Date difference in Javascript
JavaScript
var a = moment('7/11/2010','M/D/YYYY');
var b = moment('12/12/2010','M/D/YYYY');
var diffDays = b.diff(a, 'days');
alert(diffDays);
 
Share this answer
 
v2
First you have to cast the Time to integer format

C#
var Time = '<%=Session["Time"]%>';

JavaScript
var timeInt = parseInt(Time);


To get Current Date value
JavaScript
var currentDate = new Date()


To Add Minutes
JavaScript
currentDate.setMinutes(currentDate.getMinutes() + YourValue )


To create a date from the drop down values use this
C#
var d = new Date(year, month, day, hours, minutes, seconds, milliseconds);

make milliseconds and seconds as 0 rest you can get from the picker and ddls.

Compare dates
JavaScript
if( d2 > d1) alert ('something'); else  alert ('something something');
 
Share this answer
 
Comments
Member 11589429 30-Mar-16 2:35am    
Hi Kartik thanks for ur help.I am selecting date from date picker,that gives me date like this '03/30/2016',I have another dropdownlist for selecting hours and min. I want to add hours and time in selected date.Hours is in 24.Time in 60.As u said to use like this.var d = new Date(year, month, day, hours, minutes, seconds, milliseconds);Again I have to split date to get month,day and year.The code which I used is right to add min and hours in specific date.I showing 2 dates in alert box.Format of like this.One date is Wed Mar 30 2016 11:48:43 GMT+0530(India standard Time) and another is Tue Mar 29 2016 17:54:00 GMT+530(India standard Time). If (d2 > d) { alert('Scheduled date should be greater than current datetime'); }
Member 11589429 30-Mar-16 2:36am    
Does it a right approach?
Karthik_Mahalingam 30-Mar-16 2:41am    
yes, you can write this in many ways, this is one of the approach,
since we are using this cast[ Date(year, month, day, hours, minutes, seconds, milliseconds) ] it is safe and independent of the machine's datetime format.
For example if you wants to add any no. of hours to any date object.
For demonstration i will add 6.5 hours to current time.

const dt = new Date();
const newDt = new Date(dt.getTime() + (6.5 * 60 * 60 * 1000));
 
Share this answer
 
Comments
Richard Deeming 28-May-21 4:57am    
Which is exactly what solution 1 said five years ago!

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