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

IE 6 Jscript date ignores regional settings 1

Status
Not open for further replies.

harmmeijer

Programmer
Mar 1, 2001
869
CN
Here is some code checking the Outlook date format:
Code:
	this.checkDateFormat = function(){
		out = new ActiveXObject( "Outlook.Application" );
		var aItem = out.CreateItem(this.ItemType);
		aItem.Start = "5-31-2005 20:30";
		if(aItem.Start.startsWith("31"))	{
			return "dmy";
		}else{
			return "mdy";
		}
		aItem.Close(1);
	}

Same script same user gives me dmy in outlook vb editor but
mdy in IE.
Regional settings are: Dutch, short date: d-M-yyyy long date: dddd d MMMM yyyy


More suprising the following gives me 1 in vbs but 2 in java:
Code:
<script language="VBScript">
  msgbox day(cdate("01/02/2000")) ' will be 1 is correct according to locale settings
</script>
<script language="JavaScript">
  alert(new Date("01/02/2000").getDate()); // will be 2 is incorrect according to locale settings
  // try it this way then:
  alert(new Date(2000,0,2).toLocaleString().substring(0,2)); // is 01 which means that month comes before day
</script>


The problem is that I need to insert a Calendar (Appointment) item in Outlook. Not sure what the user did with locale settings so I have to check the actual locale settings.

Looked on google for this phenomenom but could not find any bug in IE saying that javascript ignores locale settings when creating a date. Current sollution is:

Code:
	this.checkDateFormat = function(){
		out = new ActiveXObject( "Outlook.Application" );
		aItem = out.CreateItem(1);
		aItem.Start="01-03-2005";
		var d = new Date(aItem.Start);
		aItem.Close(1);
		if(d.getMonth()==2){
			return "dmy";
		}else{
			return "mdy";
		}
	}



Greetings, Harm Meijer
 
could not find any bug in IE saying that javascript ignores locale settings when creating a date.

It does ignore locale settings - the MSDN documentation is quite specific about what a new Date object expects for parameters. Either:

1. Milliseconds since 1/1/1970
2. Parameters (y, m, d) (+ time params if you wish)
3. A string which takes the same format as that for the parse method, which can be a short or long date format, but as with option 2 above, must be in m/d/y format if short:

MSDN said:
Short dates can use either a "/" or "-" date separator, but must follow the month/day/year format, for example "7/20/96".

Long dates of the form "July 10 1995" can be given with the year, month, and day in any order, and the year in 2-digit or 4-digit form. If you use the 2-digit form, the year must be greater than or equal to 70.

Hope this helps,
Dan

[tt]D'ya think I got where I am today because I dress like Peter Pan here?[/tt]
[banghead]

 
Ok thank you, I get the mdy constructor for Date(String).

But this bothers me still:
toLocaleString()
Did not return the locale string according to my regional settings though:
alert(new Date(200,2,6).toLocaleString());

But as the site above suggested I should not use this to compute anything.

The most stable way is to use Outlook for telling me the date format anyway. That's were the appt is going to end up.



Greetings, Harm Meijer
 
I wouldn't expect that to return anything realistic, given that you are passing "200" for the year. Try putting this in your browser's address bar and see what you get. I get "26 June 2005 00:00:00" returned:

Code:
javascript:alert(new Date(2005, 5, 26).toLocaleString());

Dan

[tt]D'ya think I got where I am today because I dress like Peter Pan here?[/tt]
[banghead]

 
06/26/2005 00:00:00
regional long date style: dddd, MMMM dd, yyyy
short date style: M/d/yyyy
Your locale (Location) Enlish (United States)


06/26/2005 00:00:00
regional long date style: dddd d MMMM yyyy
short date style: d-M-yyyy
Your locale (Location) Dutch (Netherlands)




Greetings, Harm Meijer
 
What about using "toLocaleDateString" instead of "toLocaleString"?

Dan

[tt]D'ya think I got where I am today because I dress like Peter Pan here?[/tt]
[banghead]

 
javascript:alert((new Date(2000,2,6)).toLocaleDateString());

gets me object doesn't support property or method.


IE version 6.0.2800 128 bit on w2k pro sp 4

Can try it on another PC but have to do some other work now (drink alcoholic beveriges in the sun).

thank you for your support, I see this problem a result of my sloppy work methods and or a freak setting on this PC (maintained by Admins).




Greetings, Harm Meijer
 
harmmeijer,

If I can restrict myself to the first script, should you make sure startsWith() is correct? Is it a .net? I don't get it completely. If it error out, no wonder you get 'mdy'.

regards - tsuji
 
Ok, I can think trait now (2 heineken and some sunlight). Sorry tsuji for posting the wrong code, since it started with the wrong value to begin with I never bothered to do a .substring or .indexof with the result.

Here is the code example so far, it works now and as ms says both toLocaleString and toLocaleDateString is just for user output.

Code:
<html>
<head>
</head>
<body>

<script language="javascript">

// *****************************************************
// *****************************************************
// *****************************************************
// *****************************************************
// this can be in a js file to be included in a html page
// *****************************************************
// *****************************************************
// *****************************************************
// *****************************************************


function appt( Subject, Location, Start,End, ReminderMinutesBeforeStart,BillingInformation, toUpdate ){
	// TODO: strAddrecip is the text displayed when the user is asked if he or she wants to add recipients in theire outlook appt
	// TODO: to use Billing Information to fill out a unique number (database) so the web application can find it later
	// is not one of the most stable ways but since FileAs is not available this seems appropriate (for lack of better)
	this.strAddrecip = "You want to add the attandees in your outlook calendar?";
	this.Subject = Subject;
	this.Location  = Location;
	this.Start  = Start;
	this.End  = End;
	this.ReminderMinutesBeforeStart = ReminderMinutesBeforeStart;
	this.ItemType = 1;
	this.Recipients = new Array();
	this.BillingInformation = BillingInformation
	this.toUpdate = toUpdate
	this.AppointmentItem = null;
	this.addRecipient = function(strName){
		this.Recipients[this.Recipients.length] = strName;
	}
	this.saveInOutlook = function(){
		/* Create the Outlook Object and Appointment Item */
		// TODO: try and catch this, when caught the client
		// probably has a security restriction
		out = new ActiveXObject( "Outlook.Application" );

		// Do not insert the item if an item with the same billing information allready exist
		var f = out.GetNamespace("MAPI").GetDefaultFolder(9);
		var aItem = f.Items.Find("[BillingInformation] = \"" + this.BillingInformation + "\"");
		if(aItem!=null){
			if(this.toUpdate==0){ // item is to be inserted, check if it exist allready
				// TODO: make this a slick looking modaldialog
				alert("Item allready exist");
				return;
			}
		}


		/* Create an Appointment Item */
		if(this.toUpdate==0){
			aItem = out.CreateItem(this.ItemType);
		}else{
			aItem = this.AppointmentItem;
		}

		/* Transfer the data */
		aItem.Subject = this.Subject;
		aItem.Location  = this.Location;
		aItem.Start = this.Start;
		aItem.End = this.End;
		aItem.ReminderMinutesBeforeStart = this.ReminderMinutesBeforeStart;
		aItem.BillingInformation = this.BillingInformation;
		// TODO: replace the confirm with a slick looking modal dialog
		if(this.Recipients.length!=0){
			if(confirm(this.strAddrecip)){
				var i = 0
				while(i<this.Recipients.length){
					try{
						aItem.Recipients.Add (this.Recipients[i]).resolve;
					}catch(e){
						// TODO: make this a slick modaldialog enabling the uer for a 2nd change in getting the user to invite
						alert(e.message);
					}
					i++;
				}
				aItem.MeetingStatus = 1

				// aItem.Display();
				aItem.Send();
				return;
			}
		}
		aItem.Save();

		/* If you want to save instead of viewing it change to aItem.Save();*/

	}
	this.upDate = function(){
		// TODO: try and catch this, when caught the client
		// probably has a security restriction
		out = new ActiveXObject( "Outlook.Application" );
		var f = out.GetNamespace("MAPI").GetDefaultFolder(9);
		var aItem = f.Items.Find("[BillingInformation] = \"" + this.BillingInformation + "\"");
		if(aItem!=null){
			this.AppointmentItem = aItem;
			this.saveInOutlook();
		}else{
			// TODO: make this a slick looking modaldialog
			alert("Could not update the appointment item");
		}
	}
	this.checkDateFormat = function(){
		// TODO: try and catch this, when caught the client
		// probably has a security restriction
		out = new ActiveXObject( "Outlook.Application" );
		aItem = out.CreateItem(1);
		aItem.Start="01-03-2005";
		var d = new Date(aItem.Start);
		aItem.Close(1);
		if(d.getMonth()==2){
			return "dmy";
		}else{
			return "mdy";
		}
	}
}




// *****************************************************
// *****************************************************
// *****************************************************
// *****************************************************
// page specific code to enter the appointment in a calendar
// *****************************************************
// *****************************************************
// *****************************************************
// *****************************************************

function saveCalItem(forUpdate){
	var fru = new Number(forUpdate);
	var ds = new Date(document.getElementById("txtdteStart").value) // verry verry unwize to let a user type a date, this should be a (popup) calendar
	if(ds=="NaN"){
		alert("Value for start date is not valid");
		return;
	}

	var de = new Date(document.getElementById("txtdteEnd").value) // verry verry unwize to let a user type a date, this should be a (popup) calendar
	if(de=="NaN"){
		alert("Value for End date is not valid");
		return;
	}
	var start = ""; // string value that will be passed to Outlook serving as the start date
	var end = "";   // string value that will be passed to Outlook serving as the end date
	var subj = document.getElementById("txtSubject").value;
	var loc = document.getElementById("txtLocation").value;
	// 			check local settings, if local settings are
	//			dmy than reverse the day and month value
	var tmpCal = new appt(null,null,null,null,null,null,null);
	// TODO: getyear has been made y2k compient by MS and netscape (it now returns 4 digit number)
	// 		later it has been removed from java specs alltoghether and replaced with getFullYear but
	//		getFullYear might not be available in all browsers (IE6 has no problems with it)
	if(tmpCal.checkDateFormat()=="mdy"){
		// format is mdy
		start = new String(ds.getMonth() + 1) + "-" +  new String(ds.getDate()) + "-" + new String(ds.getFullYear()) + " " + new String(document.getElementById("lstHourStart").value) + ":" + new String(document.getElementById("lstMinutesStart").value);
		end = new String(de.getMonth() + 1) + "-" +  new String(de.getDate()) + "-" + new String(de.getFullYear()) + " " + new String(document.getElementById("lstHourEnd").value) + ":" + new String(document.getElementById("lstMinutesEnd").value);
	}else{
		start = new String(ds.getDate()) + "-" + new String(ds.getMonth() + 1)  + "-" + new String(ds.getFullYear()) + " " + new String(document.getElementById("lstHourStart").value) + ":" + new String(document.getElementById("lstMinutesStart").value);
		end = new String(de.getDate()) + "-" + new String(de.getMonth() + 1)  + "-" + new String(de.getFullYear()) + " " + new String(document.getElementById("lstHourEnd").value) + ":" + new String(document.getElementById("lstMinutesEnd").value);
	}
	// Note that the toUpdate value here is set to 1
	var o = new appt(subj,loc,start,end,"5","unique ID of the MRRS with some description",fru);
	// currently I don't want to bother with code retreiving all selected users but it is possible to do so'
	if(document.getElementById("lstUsers").value!="none"){
		// var i = 0;
		// while i < selectlist.items.........
		o.addRecipient(document.getElementById("lstUsers").value);
	}
	if(fru==1){
		o.upDate();
	}else{
		o.saveInOutlook();
	}
}
</script>




<table>
	<tr>
		<td colspan=2>
			Subject: <input type=text id="txtSubject" value="dinner">
		</td>
	</tr>
	<tr>
		<td colspan=2>
			Location: <input type=text id="txtLocation" value="home or some terras">
		</td>
	</tr>
	<tr>
		<td>
			Start Date (mdy) <input type=text id="txtdteStart" value="26 may 2005" >
		</td>
		<td>
			Time:
<select name="lstHourStart" id="lstHourStart" tabindex="1"  >
	<option value="00">00</option>
	<option value="01">01</option>
	<option value="02">02</option>
	<option value="03">03</option>
	<option value="04">04</option>
	<option value="05">05</option>
	<option value="06">06</option>
	<option value="07">07</option>
	<option value="08">08</option>
	<option value="09">09</option>
	<option value="10">10</option>
	<option selected="selected" value="11">11</option>
	<option value="12">12</option>
	<option value="13">13</option>
	<option value="14">14</option>
	<option value="15">15</option>
	<option value="16">16</option>
	<option value="17">17</option>
	<option value="18" selected>18</option>
	<option value="19">19</option>
	<option value="20">20</option>
	<option value="21">21</option>
	<option value="22">22</option>
	<option value="23">23</option>

</select>
<select name="lstMinutesStart" id="lstMinutesStart" tabindex="2" >
	<option selected="selected" value="00">00</option>
	<option value="15">15</option>
	<option value="30">30</option>
	<option value="45">45</option>

</select>

		</td>
	</tr>

	<tr>
		<td>
			End Date (mdy) <input type=text id="txtdteEnd" value="5-26-2005" >
		</td>
		<td>
			Time:
<select name="lstHourEnd" id="lstHourEnd" tabindex="1" >
	<option value="00">00</option>
	<option value="01">01</option>
	<option value="02">02</option>
	<option value="03">03</option>
	<option value="04">04</option>
	<option value="05">05</option>
	<option value="06">06</option>
	<option value="07">07</option>
	<option value="08">08</option>
	<option value="09">09</option>
	<option value="10">10</option>
	<option selected="selected" value="11">11</option>
	<option value="12">12</option>
	<option value="13">13</option>
	<option value="14">14</option>
	<option value="15">15</option>
	<option value="16">16</option>
	<option value="17">17</option>
	<option value="18">18</option>
	<option value="19" selected>19</option>
	<option value="20">20</option>
	<option value="21">21</option>
	<option value="22">22</option>
	<option value="23">23</option>
</select>
<select name="lstMinutesEnd" id="lstMinutesEnd" tabindex="2" >
	<option selected="selected" value="00">00</option>
	<option value="15">15</option>
	<option value="30">30</option>
	<option value="45">45</option>
</select>

		</td>
	</tr>
	<tr>
		<td colspan=2>
		<select name="lstUsers" size="4" id="lstUsers">
			<option value="none" selected>none</option>
			<option value="some.name@somecompany.com">Harm Meijer</option>
		</select>

		</td>
	</tr>

</table>




<input type=button onclick="saveCalItem(0);" value="Click here to add the item to your calendar" />
<input type=button onclick="saveCalItem(1);" value="Click here to update the item to your calendar" />
</body>
</html>



Greetings, Harm Meijer
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top