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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

confused with date 1

Status
Not open for further replies.

markemark

Instructor
Nov 11, 2003
21
GB
I have a text box where the user enters a date in english format dd/mm/yyyy and when I run the following function

<SCRIPT language=&quot;JavaScript&quot;>
function AddDays(startdate,inc) {
DaysToAdd=inc;
alert (&quot;startdate&quot;+startdate+&quot; inc &quot;+inc);
var now=new Date(startdate);
alert (now);
var newdate=new Date();
var newtimems=newdate.getTime()+(DaysToAdd*24*60*60*1000);
newdate.setTime(newtimems);
Adddays=newdate.toLocaleString();
alert (Adddays);
}
// End -->
</SCRIPT>

at the line var now=newdate(startdate); the result is american date. ie mm/dd/yyyy

so if the startdate value is 03/11/2003 then the now value is 11/03/2003 if inc =0

can anyone help me as I have no hair left after a violent pulling spree!!

Regards

Mark

ps. the objective of the function is to add a variable inc number of days to the startdate and return the results.
 
Here is a parseDate function that I extract from jscalendar. Credit to
It can parses a date string in any format defined in argument &quot;fmt&quot;.

for example

to parse a date string with format dd/mm/yyyy
Code:
var aDate = parseDate (&quot;03/11/2003&quot;, &quot;%d/%m/%Y&quot;);

to parse the date string in format 'mmm dd yyyy'
Code:
var aDate = parseDate (&quot;Nov 3 2003&quot;, &quot;%b %d %Y&quot;);


Code:
function parseDate (str, fmt) {
	var y = 0;
	var m = -1;
	var d = 0;
	var a = str.split(/\W+/);
	var b = [];
	fmt.replace(/(%.)/g, function(str, par) {
		return b[b.length] = par;
	});
	var i = 0, j = 0;
	var hr = 0;
	var min = 0;
	for (i = 0; i < a.length; ++i) {
		if (b[i] == &quot;%a&quot; || b[i] == &quot;%A&quot;) {
			continue;
		}
		if (b[i] == &quot;%d&quot; || b[i] == &quot;%e&quot;) {
			d = parseInt(a[i], 10);
		}
		if (b[i] == &quot;%m&quot;) {
			m = parseInt(a[i], 10) - 1;
		}
		if (b[i] == &quot;%Y&quot; || b[i] == &quot;%y&quot;) {
			y = parseInt(a[i], 10);
			(y < 100) && (y += (y > 29) ? 1900 : 2000);
		}
		if (b[i] == &quot;%b&quot; || b[i] == &quot;%B&quot;) {
			for (j = 0; j < 12; ++j) {
				if (Calendar._MN[j].substr(0, a[i].length).toLowerCase() == a[i].toLowerCase()) { m = j; break; }
			}
		} else if (/%[HIkl]/.test(b[i])) {
			hr = parseInt(a[i], 10);
		} else if (/%[pP]/.test(b[i])) {
			if (/pm/i.test(a[i]) && hr < 12)
				hr += 12;
		} else if (b[i] == &quot;%M&quot;) {
			min = parseInt(a[i], 10);
		}
	}
	if (y != 0 && m != -1 && d != 0) {
		return new Date(y, m, d, hr, min, 0);
	}
	y = 0; m = -1; d = 0;
	for (i = 0; i < a.length; ++i) {
		if (a[i].search(/[a-zA-Z]+/) != -1) {
			var t = -1;
			for (j = 0; j < 12; ++j) {
				if (Calendar._MN[j].substr(0, a[i].length).toLowerCase() == a[i].toLowerCase()) { t = j; break; }
			}
			if (t != -1) {
				if (m != -1) {
					d = m+1;
				}
				m = t;
			}
		} else if (parseInt(a[i], 10) <= 12 && m == -1) {
			m = a[i]-1;
		} else if (parseInt(a[i], 10) > 31 && y == 0) {
			y = parseInt(a[i], 10);
			(y < 100) && (y += (y > 29) ? 1900 : 2000);
		} else if (d == 0) {
			d = a[i];
		}
	}
	if (y == 0) {
		var today = new Date();
		y = today.getFullYear();
	}
	if (m != -1 && d != 0) {
		return new Date(y, m, d, hr, min, 0);
	}
}

/*
Here is a list of date format characters accept by the date parser

%a  abbreviated weekday name  
%A  full weekday name  
%b  abbreviated month name  
%B  full month name  
%C  century number  
%d  the day of the month ( 00 .. 31 )  
%e  the day of the month ( 0 .. 31 )  
%H  hour ( 00 .. 23 )  
%I  hour ( 01 .. 12 )  
%j  day of the year ( 000 .. 366 )  
%k  hour ( 0 .. 23 )  
%l  hour ( 1 .. 12 )  
%m  month ( 01 .. 12 )  
%M  minute ( 00 .. 59 )  
%n  a newline character  
%p  “PM” or “AM”  
%P  “pm” or “am”  
%S  second ( 00 .. 59 )  
%s  number of seconds since Epoch (since Jan 01 1970 00:00:00 UTC)  
%t  a tab character  
%U, %W, %V  the week number 
%u  the day of the week ( 1 .. 7, 1 = MON ) 
%w  the day of the week ( 0 .. 6, 0 = SUN ) 
%y  year without the century ( 00 .. 99 ) 
%Y  year including the century ( ex. 1979 ) 
%%  a literal % character  

*/
 
Thanks for that :), but just to verify, I can call the parseDate function and the varible I store the result in, I can now pass to my own AddDays function?

Mark
 
you are right, you can now pass to your own AddDays function.

I've missed something in the above code. You will also need the following constants for parseDate to work with month name etc.

Code:
Calendar = function (){
}

Calendar._DN = new Array
(&quot;Sunday&quot;,
 &quot;Monday&quot;,
 &quot;Tuesday&quot;,
 &quot;Wednesday&quot;,
 &quot;Thursday&quot;,
 &quot;Friday&quot;,
 &quot;Saturday&quot;,
 &quot;Sunday&quot;);

// short day names
Calendar._SDN = new Array
(&quot;Sun&quot;,
 &quot;Mon&quot;,
 &quot;Tue&quot;,
 &quot;Wed&quot;,
 &quot;Thu&quot;,
 &quot;Fri&quot;,
 &quot;Sat&quot;,
 &quot;Sun&quot;);

// full month names
Calendar._MN = new Array
(&quot;January&quot;,
 &quot;February&quot;,
 &quot;March&quot;,
 &quot;April&quot;,
 &quot;May&quot;,
 &quot;June&quot;,
 &quot;July&quot;,
 &quot;August&quot;,
 &quot;September&quot;,
 &quot;October&quot;,
 &quot;November&quot;,
 &quot;December&quot;);

// short month names
Calendar._SMN = new Array
(&quot;Jan&quot;,
 &quot;Feb&quot;,
 &quot;Mar&quot;,
 &quot;Apr&quot;,
 &quot;May&quot;,
 &quot;Jun&quot;,
 &quot;Jul&quot;,
 &quot;Aug&quot;,
 &quot;Sep&quot;,
 &quot;Oct&quot;,
 &quot;Nov&quot;,
 &quot;Dec&quot;);
 
does this get added to the previous function or is it a new one?
 
The second code are separate functions that may be called by the fist one if month name is used in the date format.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top