开发者

Javascript date calculation

I have date field,Onclick on date feild,i have to calculate the date with the current date .if entered date is one month older than today date.I have to popup amessage saying date开发者_开发百科 is olderthan 1 month.

Date format is "yyyy-MM-dd".i want to compare with date "1984-03-20" to today date "2011-04-14".it should return number of months.

Any hints on this. Regards,

Chaitu


See this discussion it has demos for getting date difference in javascript. Also this is a good example: javascript-date-difference-calculation it shows how to get difference in month, in week or in year.


Maybe this could help to you:

http://www.ozzu.com/programming-forum/javascript-date-comparison-t46552.html

<script language="JavaScript">

var days = 0;
var difference = 0;

Christmas = new Date("December 25, 2005");

today = new Date();

difference = Christmas - today;

days = Math.round(difference/(1000*60*60*24));

</script>


The easiest way to compare dates in javascript is to first convert it to a Date object and then compare these date-objects.

Below you find an object with below function:

dates.inRange (d,start,end)

Returns a boolean or NaN:

    * true if d is between the start and end (inclusive)
    * false if d is before start or after end.
    * NaN if one or more of the dates are illegal.


var dt = new Date('02 Jul 2009');
dt.setMonth(dt.getMonth() – 1);

You can read up about js Date object to see what formats it can accept.

A blog about JS Date formats


You can do something like that:

   <script type="text/javascript">

        function checkdate (){

           var today = new Date()

           var dating = document.getElementById("date").attr("value");

           var new = new Date(dating);

           var diff = today - new;

           var days = Math.round(diff/(1000*60*60*24));

           if (days >= 30){

                alert("more than one month!");
           }
      }

    </script>

    <html><body>
       <input type="text" id="date" onClick="checkdate();"/>
    </html></body>


This would work. Pass the value of the field to dateString onclick and the code will determine whether the date is older than 1 month or not.

var dateString = '3/26/2011';
var selectDate = new Date(dateString); // convert string to date
var monthOld = new Date();

monthOld.setDate(monthOld.getDate() - 30); // subtract 30 days

if (selectDate < monthOld) {
    alert('older than one month');
}
else alert('ok');

Demo: http://jsfiddle.net/cw4vb/


Firstly I would advise you to adjust your requirement to 30 days. This is probably fine for your users and is unambiguous for both you and them. 1 month can be of varying length depending on the time of year.

for solution start with

var millisecond = 1;
var second = 1000;
var minute = second * 60;
var hour = minute * 60;
var day = hour * 24;
var week = day* 7;

then on date entry do this

if (usersDate < new Date().getTime() - 30 * day) {
   //Do popup / whatever
}

Alternatively if you really want to do month based interval then do

var now = new Date();
if (usersDate < now.setMonth(now.getMonth() - 1))) {
 //Do popup / whatever
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜