Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Javascript - convert string to date variable

Posted on 2009-03-30
1
Medium Priority
?
4,760 Views
Last Modified: 2012-05-06
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
Comment
Question by:josephdaviskcrm
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 19

Accepted Solution

by:
Albert Van Halen earned 2000 total points
ID: 24019885
toDateString is a method.
Use it with parentehesis.
.toDateString()

Open in new window

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question