Well, I had a reply, but it seems to have been lost. Apologies if this
turns out to be a double post.
mg was asking for some javascript examples...
This is how I'd probably handle it, in an HTML test harness so folks without
InfoPath can see it work:
<html>
<head>
<title>Test</title>
<script language="javascript">
Date.prototype.toShortDateString = function()
{
var YYYY,MM,M,DD,D;
YYYY = this.getFullYear() + "";
MM = (M=this.getMonth()+1)<10?('0'+M):M;
DD = (D=this.getDate())<10?('0'+D)
;
return MM + "/" + DD + "/" + YYYY ;
}
function parseISODate(sDate)
{
if (sDate.match(/(\d\d\d\d)-(\d\d)-(\d\d)/))
{
return Date.parse(RegExp.$2 + "/" + RegExp.$3 + "/" + RegExp.$1);
}
else
{
return Date.parse(sDate)
}
}
function AddDays(oldDate, daysToAdd)
{
var t1, t2, newDate;
var MinMilli = 1000 * 60;
var HrMilli = MinMilli * 60;
var DyMilli = HrMilli * 24;
t1 = parseISODate(oldDate);
t2 = t1 + Math.round(daysToAdd * DyMilli)
newDate = new Date(t2)
return(newDate.toShortDateString());
}
function init()
{
var dateString1 = "2005-01-25T12:31:48.123Z";
// var dateString1 = "2005-01-25"
// var dateString1 = "01-25-2005";
// var dateString1 = "2005/01/25";
var sHtml = AddDays(dateString1, 10)
target.innerText = sHtml
}
window.onload = init;
</script>
<body>
<h2 id="target"></h2>
</body>
</html>
There is an intrinsic Date object with a static parse method. This,
unfortunately, doesn't accept ISO format dates. However, this handy line of
code:
Object.prototype.parseISODate = function(sDate)
extends the instrinsic Date object with a method that handles both standard
and ISO format dates. Actually, you'd want to make that function a bit more
rigorous, so that it would handle full ISO format dates and return just what
it finds. So if it found
The other odd bit near the top:
Date.prototype.toShortDateString = function()
is one way of extending the date object to serialize into a string format
that it doesn't support. I have a variety of these little methods I use.
For example, I have one that produces an ISO 8601 format that will validate
in a W3C XML Schema. This one works on a Date object instance. The other
one is a prototype of the Object object, which the Date object "inherits".
Actually, if we're going to do a regex, we should probably make it bit more
robust, and extend these so they can handle ISO short and long dates. The
regex below will also ignore dates like 2004-35-88 (though obviously it
doesn't
check for leap years, and whether April has 31 days...That checking is left
to the Date object which will validate the date string anyway.
Here's the same example extended a bit more (no guarantees on robustness--I
only did a little testing ;^)
<html>
<head>
<title>Test</title>
<script language="javascript">
Date.prototype.toShortDateString = function()
{
var YYYY,MM,M,DD,D;
YYYY = this.getFullYear() + "";
MM = (M=this.getMonth()+1)<10?('0'+M):M;
DD = (D=this.getDate())<10?('0'+D)
;
return MM + "/" + DD + "/" + YYYY ;
}
Date.prototype.toDateTimeString = function()
{
var YYYY,MM,M,DD,D,hh,h,mm,m,ss,s,i,ii,iii;
YYYY = this.getFullYear() + "";
MM = (M=this.getMonth()+1)<10?('0'+M):M;
DD = (D=this.getDate())<10?('0'+D)
;
h=this.getHours();
hh = h<10?('0'+h):h;
mm=(m=this.getMinutes())<10?('0'+m):m;
ss=(s=this.getSeconds())<10?('0'+s):s;
iii=(ii=((i=this.getMilliseconds())<10?('0'+i):i))<100?('0'+ii):i;
return YYYY + "-" + MM + "-" + DD + " " + hh + ":" + mm + ":" + ss + "."
+ iii
}
Object.prototype.parseISODate = function(sDate)
{
if
(sDate.match(/(\d{4})-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])T(0[0-9]|1[0-9
]|2[0-4])
0[0-9]|[1-5][0-9])
0[0-9]|[1-5][0-9]).(\d{3})(Z)/))
{
var t = Date.parse(RegExp.$2 + "/" + RegExp.$3 + "/" + RegExp.$1 + "
" + RegExp.$4 + ":" + RegExp.$5 + ":" + RegExp.$6);
if (RegExp.$7 != "")
t += parseInt(RegExp.$7);
return t
}
else
{
if (sDate.match(/(\d{4})-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])/))
return Date.parse(RegExp.$2 + "/" + RegExp.$3 + "/" +
RegExp.$1);
else
return Date.parse(sDate)
}
}
function AddDays(oldDate, daysToAdd)
{
var t1, t2, newDate, tempDate;
tempDate = new Date();
var MinMilli = 1000 * 60;
var HrMilli = MinMilli * 60;
var DyMilli = HrMilli * 24;
t1 = Date.parseISODate(oldDate);
t2 = t1 + Math.round(daysToAdd * DyMilli)
newDate = new Date(t2)
// return(newDate.toShortDateString());
return(newDate.toDateTimeString());
}
function init()
{
var dateString1 = "2005-01-25T12:31:48.123Z";
// var dateString1 = "2005-01-25"
// var dateString1 = "01-25-2005";
// var dateString1 = "2005/01/25";
var sHtml = AddDays(dateString1, 7)
target.innerText = sHtml
}
window.onload = init;
</script>
<body>
<h2 id="target"></h2>
</body>
</html>
Regards,
Mike Sharp