aspose file tools*
The moose likes HTML, CSS and JavaScript and the fly likes Problem with javascript dates Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Problem with javascript dates" Watch "Problem with javascript dates" New topic
Author

Problem with javascript dates

Sree Mami
Ranch Hand

Joined: Jul 13, 2006
Posts: 75
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

Joined: Nov 08, 2001
Posts: 15376
    
    6
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

Joined: Jul 13, 2006
Posts: 75
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

Joined: Nov 24, 2005
Posts: 14688
    
  16

Why "new Cal" ?
Eric proposed "new Date"


[My Blog]
All roads lead to JavaRanch
Sree Mami
Ranch Hand

Joined: Jul 13, 2006
Posts: 75
Sorry it was new Date(document.formname.elementname.value) it was a mistake in my typing
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10265
    
168

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:


[My Blog] [JavaRanch Journal]
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10265
    
168

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


Post an example
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
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

Joined: Jul 13, 2006
Posts: 75
Then, can't we use UK Date format for date calculations?
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
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

Joined: Jul 13, 2006
Posts: 75
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

Joined: Nov 08, 2001
Posts: 15376
    
    6
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

Joined: Jul 13, 2006
Posts: 75
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 ]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Problem with javascript dates