如何将min添加到当前日期/特定日期并在javascript中比较两个日期? [英] How to add min to current date/specific date and compare both dates in javascript?

查看:96
本文介绍了如何将min添加到当前日期/特定日期并在javascript中比较两个日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

会话包含时间(分钟)。正面或负面。我想在当前日期添加该时间。如果是正面的话,它应该被添加。如果是负面的,它应该从那里减少。我想从javascript中做到这一点。



1:

  var 时间= ' <%=会话[时间]%>'; 
var d1 = new 日期(),
d2 = new 日期(d1);
d2.setMinutes(d1.getMinutes()+ Time);
alert(d2);



当我写这样的代码(上面代码)时,我的日期无效。



  var 时间= ' <%=会话[ 时间]%GT;'; 
var d1 = new 日期(),
d2 = new 日期(d1);
d2.setMinutes(d1.getMinutes()+ 30 );
alert(d2);



当我这样写时(上面的代码)我得到了正确的日期。这是一个正确的方法来添加min当前约会。



2:我从日期选择器中选择日期,这给我这样的日期'03 / 30/2016',我还有另一个选择小时的下拉列表分钟。我想在所选日期中添加小时和时间。小时是在24.时间在60.



  var  d =  new 日期('  2016年3月30日); 
d.setHours(d.getHours()+ 17 );
d.setMinutes(d.getMinutes()+ 54 );
alert(d);



这是一种正确的方法吗?



3:我想比较日期d和d2。 d应该大于d2。如果不是,应该向用户显示消息。



我尝试过:



  var 时间= ' <%=会话[时间]%>'; 
var d1 = new 日期(),
d2 = new 日期(d1);
d2.setMinutes(d1.getMinutes()+ Time);
alert(d2);



当我写这样的代码(上面的代码)时,我的日期无效。



  var 时间= ' <%=会话[ 时间]%GT;'; 
var d1 = new 日期(),
d2 = new 日期(d1);
d2.setMinutes(d1.getMinutes()+ 30 );
alert(d2);

解决方案

以下是一些小片段,您可以将它们分组以完成您的任务



- 在javascript中添加日期分钟

  function  addMinutes (日期,分钟)
{
return new 日期(date.getTime()+分钟* 60000);
}
//
addMinutes( new 日期 2014 10 2 ),60 * 24)



- 从日期减去分钟数

  var  endDate = somedate; 
var startdate = new 日期(结束日期);
var durationInMinutes = 20 ;
startdate.setMinutes(endDate.getMinutes() - durationInMinutes);



- Javascript中的日期差异

  var  a =时刻('  7/11 / 2010''  M / D / YYYY'); 
var b =时刻(' 12 / 12/2010'' M / D / YYYY');
var diffDays = b.diff(a,' );
alert(diffDays);


首先你需要施放时间到整数格式



  var  Time = ' <%= Session [Time]%>'; 



  var  timeInt =  parseInt < /跨度>(时间); 





获取当前日期值

  var  currentDate =  new  日期()





添加分钟

 currentDate.setMinutes(currentDate.getMinutes()+ YourValue )





要从下拉值创建日期,请使用此

  var  d =  new 日期(年,月,日,小时,分钟,秒,毫秒); 



将毫秒和秒作为0,你可以从选择器和ddls获得休息。



比较日期

 如果(d2> d1)警告(' 某事');  else  alert(' 某事') ; 


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 :

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.

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.

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:

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.

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

解决方案

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

- Add minutes in date in 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

var endDate = somedate;
var startdate = new Date(endDate);
var durationInMinutes = 20;
startdate.setMinutes(endDate.getMinutes() - durationInMinutes);


- Date difference in 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);


First you have to cast the Time to integer format

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


var timeInt = parseInt(Time);



To get Current Date value

var currentDate = new Date()



To Add Minutes

currentDate.setMinutes(currentDate.getMinutes() + YourValue )



To create a date from the drop down values use this

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

if( d2 > d1) alert ('something'); else  alert ('something something');


这篇关于如何将min添加到当前日期/特定日期并在javascript中比较两个日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆