• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Problem with javascript dates

 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why "new Cal" ?
Eric proposed "new Date"
 
Sree Mami
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry it was new Date(document.formname.elementname.value) it was a mistake in my typing
 
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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



Post an example
 
Eric Pascarello
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Then, can't we use UK Date format for date calculations?
 
Eric Pascarello
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
reply
    Bookmark Topic Watch Topic
  • New Topic