2

How would I achieve the pseudo-code below in JavaScript? I want to include the date check in the second code excerpt, where txtDate is for the BilledDate.

If ABS(billeddate – getdate)  >  31 then yesno “The date you have entered is more than a month from today, Are you sure the date is correct,”.


if (txtDate && txtDate.value == "")
{
    txtDate.focus();
    alert("Please enter a date in the 'Date' field.")
    return false;
}

4 Answers 4

2

Generally speaking you work with Date-objects in javascript, and these should be constructed with the following syntax:

    var myDate = new Date(yearno, monthno-1, dayno);
    //you could put hour, minute, second and milliseconds in this too

Beware, the month-part is an index, so january is 0, february is 1 and december is 11 !-)

Then you can pull out anything you want, the .getTime() thing returns number of milliseconds since start of Unix-age, 1/1 1970 00:00, så this value you could subtract and then look if that value is greater than what you want:

//today (right now !-) can be constructed by an empty constructor
var today = new Date();
var olddate = new Date(2008,9,2);
var diff = today.getTime() - olddate.getTime();
var diffInDays = diff/(1000*60*60*24);//24 hours of 60 minutes of 60 second of 1000 milliseconds

alert(diffInDays);

This will return a decimal number, so probably you'll want to look at the integer-value:

alert(Math.floor(diffInDays));
Sign up to request clarification or add additional context in comments.

Comments

1

To get the date difference in days in plain JavaScript, you can do it like this:

var billeddate = Date.parse("2008/10/27");
var getdate = Date.parse("2008/09/25");

var differenceInDays = (billeddate - getdate)/(1000*60*60*24)

However if you want to get more control in your date manipulation I suggest you to use a date library, I like DateJS, it's really good to parse and manipulate dates in many formats, and it's really syntactic sugar:

// What date is next thrusday?
Date.today().next().thursday();
//or
Date.parse('next thursday');

// Add 3 days to Today
Date.today().add(3).days();

// Is today Friday?
Date.today().is().friday();

// Number fun
(3).days().ago();

Comments

0

You can use this to check for valid date

function IsDate(testValue) {

        var returnValue = false;
        var testDate;
        try {
            testDate = new Date(testValue);
            if (!isNaN(testDate)) {
                returnValue = true;            
            }
            else {
                returnValue = false;
            }
        }
        catch (e) {
            returnValue = false;
        }
        return returnValue;
    }

And this is how you can manipulate JS dates. You basically create a date object of now (getDate), add 31 days and compare it to the date entered

function IsMoreThan31Days(dateToTest) {

  if(IsDate(futureDate)) {
      var futureDateObj = new Date();
      var enteredDateObj = new Date(dateToTest);

      futureDateObj.setDate(futureDateObj.getDate() + 31); //sets to 31 days from now.
      //adds hours and minutes to dateToTest so that the test for 31 days is more accurate.
      enteredDateObj.setHours(futureDateObj.getHours()); 
      enteredDateObj.setMinutes(futureDateObj.getMinutes() + 1);

      if(enteredDateObj >= futureDateObj) {
        return true;
      }
      else {
        return false;
      }
   }
}

Comments

-2

Hello and good day for everyone

You can try Refular Expressions to parse and validate a date format

here is an URL yoy can watch some samples and how to use

http://www.javascriptkit.com/jsref/regexp.shtml

A very very simple pattern would be: \d{2}/\d{2}/\d{4}

for MM/dd/yyyy or dd/MM/yyyy

With no more.... bye bye

2 Comments

dont use string operations for math
I'm trying to validate the value of a correctly formatted date.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.