Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Date functions 1

Status
Not open for further replies.

craigward

Programmer
Nov 13, 2007
230
GB
Hi, I have two fields, date a and date b formatted like dd/mm/yyyy

Is there be a JavaScript function out there that will allow me to work out the exact amount of day between the two date fields?

Any advice would be great as I can’t find any working examples out there.

For example

Date a = 01/01/2010

Date b = 23/01/2010

Answer = 22 days
 
Hi

Code:
[teal]([/teal][b]new[/b] [COLOR=darkgoldenrod]Date[/color][teal]([/teal][green][i]'23/01/2010'[/i][/green][teal].[/teal][COLOR=darkgoldenrod]replace[/color][teal]([/teal][fuchsia]/(\d+)\/(\d+)\/(\d+)/[/fuchsia][teal],[/teal][green][i]'$3-$2-$1'[/i][/green][teal])).[/teal][COLOR=darkgoldenrod]getTime[/color][teal]()-[/teal][b]new[/b] [COLOR=darkgoldenrod]Date[/color][teal]([/teal][green][i]'01/01/2010'[/i][/green][teal].[/teal][COLOR=darkgoldenrod]replace[/color][teal]([/teal][fuchsia]/(\d+)\/(\d+)\/(\d+)/[/fuchsia][teal],[/teal][green][i]'$3-$2-$1'[/i][/green][teal])).[/teal][COLOR=darkgoldenrod]getTime[/color][teal]())/[/teal][purple]24[/purple][teal]/[/teal][purple]60[/purple][teal]/[/teal][purple]60[/purple][teal]/[/teal][purple]1000[/purple]

Feherke.
 
Thanks for the reply, i'm trying to put it into a test environment so i can understand what is happening but the alert i get back is NaN

Am i doing something wrong?


Code:
<html>

<head>
<script langauge="JavaScript">


function test(){

a = (new Date('23/01/2010'.replace(/(\d+)\/(\d+)\/(\d+)/,'$3-$2-$1')).getTime()-new Date('01/01/2010'.replace(/(\d+)\/(\d+)\/

(\d+)/,'$3-$2-$1')).getTime())/24/60/60/1000

alert(a)

}

</script>


</head>

<body onLoad="test()">

</body>


</html>
 
Hi

The regular expression must be on one line.
Code:
[teal]([/teal]
  [b]new[/b] [COLOR=darkgoldenrod]Date[/color][teal]([/teal]                  [gray]// instantiate a date[/gray]
    [green][i]'23/01/2010'[/i][/green][teal].[/teal][COLOR=darkgoldenrod]replace[/color][teal]([/teal]    [gray]// transform the string[/gray]
      [fuchsia]/(\d+)\/(\d+)\/(\d+)/[/fuchsia][teal],[/teal] [gray]//   from dd/mm/yyyy[/gray]
      [green][i]'$3-$2-$1'[/i][/green]             [gray]//   into yyyy-mm-dd[/gray]
    [teal])[/teal]
  [teal]).[/teal][COLOR=darkgoldenrod]getTime[/color][teal]()[/teal]                [gray]// date as milliseconds since epoch[/gray]
  [teal]-[/teal]                          [gray]// subtract the two integers[/gray]
  [b]new[/b] [COLOR=darkgoldenrod]Date[/color][teal]([/teal]                  [gray]// instantiate a date[/gray]
    [green][i]'01/01/2010'[/i][/green][teal].[/teal][COLOR=darkgoldenrod]replace[/color][teal]([/teal]    [gray]// transform the string[/gray]
      [fuchsia]/(\d+)\/(\d+)\/(\d+)/[/fuchsia][teal],[/teal] [gray]//   from dd/mm/yyyy[/gray]
      [green][i]'$3-$2-$1'[/i][/green]             [gray]//   into yyyy-mm-dd[/gray]
    [teal])[/teal]
  [teal]).[/teal][COLOR=darkgoldenrod]getTime[/color][teal]()[/teal]                [gray]// date as milliseconds since [/gray]
[teal])/[/teal][purple]24[/purple][teal]/[/teal][purple]60[/purple][teal]/[/teal][purple]60[/purple][teal]/[/teal][purple]1000[/purple]              [gray]// get days from milliseconds[/gray]

Feherke.
 
Thank you for the detailed notes, that helps a lot as i have not used regular expression before.

I am having issues getting a result and it is probably me being stupid but the expression is all on one row but the alert i get is still NaN.

Any ideas? Thanks


Code:
<html>

<head>
<script langauge="JavaScript">


function test(){

a = (new Date('23/01/2010'.replace(/(\d+)\/(\d+)\/(\d+)/,'$3-$2-$1')).getTime()-new Date('01/01/2010'.replace(/(\d+)\/(\d+)\/(\d+)/,'$3-$2-$1')).getTime()               
)/24/60/60/1000              

alert(a)

}

</script>


</head>

<body onLoad="test()">

</body>


</html>
 
Hi

Weird. The [tt]Date[/tt] object does not handle dd/mm/yyyy format, so I transformed it into yyyy-mm-dd, which is ISO standard. I usually test with FireFox and Chromium, because those have advanced debuggers. And both worked. But other browsers with the same engine, for example SeaMonkey and Midori throw errors.

The mm/dd/yyyy, the English format, is handled correctly in all browsers I have installed :
Code:
[teal]([/teal][b]new[/b] [COLOR=darkgoldenrod]Date[/color][teal]([/teal][green][i]'23/01/2010'[/i][/green][teal].[/teal][COLOR=darkgoldenrod]replace[/color][teal]([/teal][fuchsia]/(\d+)\/(\d+)/[/fuchsia][teal],[/teal][green][i]'$2/$1'[/i][/green][teal])).[/teal][COLOR=darkgoldenrod]getTime[/color][teal]()-[/teal][b]new[/b] [COLOR=darkgoldenrod]Date[/color][teal]([/teal][green][i]'01/01/2010'[/i][/green][teal].[/teal][COLOR=darkgoldenrod]replace[/color][teal]([/teal][fuchsia]/(\d+)\/(\d+)/[/fuchsia][teal],[/teal][green][i]'$2/$1'[/i][/green][teal])).[/teal][COLOR=darkgoldenrod]getTime[/color][teal]())/[/teal][purple]24[/purple][teal]/[/teal][purple]60[/purple][teal]/[/teal][purple]60[/purple][teal]/[/teal][purple]1000[/purple]
Sorry for making you to run extra rounds.

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top