Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 4761
  • Last Modified:

Javascript - convert string to date variable

I'm trying to convert a string to a date object in javascript and then compare those two objects to make sure they create a valid date range, ie: start date is earlier than the end date.  I've pasted my function in the code window.

I'm writing the DateObject.toDateString back into the text boxes for testing as the date comparison has turned out to be false no matter what dates were passed to it.  I found that when the dateObject was converted back into a string and placed in the textbox, that I was getting something very strange: "function toDateString() {    [native code]}"

So aparently, my conversion from string to date object isn't working correctly to begin with.  The text being passed in these text boxes is first validated to see if there are actually valid dates.  The text turns out to be something like this: 03/23/2009

Any ideas why this wouldn't be converting correctly to a date object in the code that I have posted here?
function validateDateRange(oSrc, args) {
    var StartDate = new Date(document.getElementById('rptcntrlr_bdpStartDate_textBox').value);
    var EndDate = new Date(document.getElementById('rptcntrlr_bdpEndDate_textBox').value);
 
    document.getElementById('rptcntrlr_bdpStartDate_textBox').value = StartDate.toDateString
    document.getElementById('rptcntrlr_bdpEndDate_textBox').value = EndDate.toDateString
    
 
    if (document.getElementById('rptcntrlr_bdpStartDate_textBox').value != "" && document.getElementById('rptcntrlr_bdpEndDate_textBox').value != "") {
        args.IsValid = (StartDate < EndDate);
    }
    else {
        args.IsValid = true;
    }
}

Open in new window

0
josephdaviskcrm
Asked:
josephdaviskcrm
1 Solution
 
Albert Van HalenCommented:
toDateString is a method.
Use it with parentehesis.
.toDateString()

Open in new window

0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now