This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem with javascript dates

 
Sree Mami
Ranch Hand
Posts: 75
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all

I am using datetimepicker tool and getting dates from it into text fields. I have two dates I have to compare these two dates, which are in the text field and have to return difference of dates and alert message if start date is greater than end date and all...

I am using javascript function



Here I am getting error saying; startdate.getTime is not a valid function. Can anyone help me out how to retrieve the Textfield value as Date object
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because you are using a string. Remember that JavaScript has loose variables. Just because you initally set it to a Date() object does not mean that is what it will be when you set it with something else.

The proper way is to do:

var date1 = new Date(document.formName.elementName.value);

and to compare it is as simple as

var diff = date2 - date1;

Eric
 
Sree Mami
Ranch Hand
Posts: 75
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Eric

I tried using


But when I am printing d1 I am getting invalid date.

How to solve this problem?

Even I have one more doubt when I add an integer value to my d1 I need to increment the date by those integer number of days.

To generate these days I am using datetimepicker.js tool
 
Christophe Verré
Sheriff
Pie
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why "new Cal" ?
Eric proposed "new Date"
 
Sree Mami
Ranch Hand
Posts: 75
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry it was new Date(document.formname.elementname.value) it was a mistake in my typing
 
Jaikiran Pai
Marshal
Pie
Posts: 10444
227
IntelliJ IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Even I have one more doubt when I add an integer value to my d1 I need to increment the date by those integer number of days.


You can do this:
 
Jaikiran Pai
Marshal
Pie
Posts: 10444
227
IntelliJ IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But when I am printing d1 I am getting invalid date.


Post an example
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But when I am printing d1 I am getting invalid date.

I bet you are using DD/MM/YYYY instead of MM/DD/YYYY

Look at the function I posted here: http://www.webdeveloper.com/forum/showthread.php?p=384325#post384325

Eric
 
Sree Mami
Ranch Hand
Posts: 75
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then, can't we use UK Date format for date calculations?
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, JavaScript only handles certain date formats and that is not one of them as you already have seen. Hence why I have given you all of this code that does the work for you. It is not that hard to use it!

Eric
 
Sree Mami
Ranch Hand
Posts: 75
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Eric

I am already using that code which you have given me and it is working fine. Just I want to know more information on, why it is in US format but not in UK format when comparing/working on dates dates!

I posted my last reply just to improve my knowledge in javascript and the link which you have given me solved all my troubles..

Thanks for all your timely help...


 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The date constructor only accepts certain formats. They are

Date() - Used for current date or create an instance of the date object
Date(dateString) - Sepcify a Date/ String format is "month day, year hours:minutes:seconds" or "MM/DD/YY HH:MM:SS".
Date(year, month, day, hours, minutes, seconds) - Create an instance of date with the specified values. Can be just (year, month, date)

Eric
 
Sree Mami
Ranch Hand
Posts: 75
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Eric

The below function returns incrementedDate value in UTC format, if at all I want to change it to US format, how should I be doing that?




Actually I have to add number of days to a day and need to display the new date in US/UK Format, but when I am using this function I am getting the value in UTC format, how can I convert then?

Please help me out..
[ August 28, 2006: Message edited by: Sree Mami ]
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic