/    Sign up×
Community /Pin to ProfileBookmark

Needs help to change date input to yyyymmdd

Hi everone,

I have this script for a due date (pregnancy) calculator. But I want to change the date input from mm/dd/yyyy to yyyymmdd. Notice the slashes are gone. If that is difficult, it would be ok with yyyy-mm-dd. The date presentation I have fixed already, but the date input I cannot manage. I am a javascript noob! Can somebody please help?

[CODE]<HEAD>
<SCRIPT LANGUAGE=”JavaScript”>
function isValidDate(dateStr) {
var datePat = /^(d{1,2})(/|-)(d{1,2})2(d{4})$/;
var matchArray = dateStr.match(datePat);
if (matchArray == null) {
alert(“Ogiltigt datumformat.”)
return false;
}

year = matchArray[4];
month = matchArray[1];
day = matchArray[3];

if (month < 1 || month > 12) {
alert(“Månad måste vara 1 – 12.”);
return false;
}
if (day < 1 || day > 31) {
alert(“Dag måste vara 1 – 31.”);
return false;
}
if ((month==4 || month==6 || month==9 || month==11) && day==31) {
alert(“Månaden “+month+” har inte 31 dagar!”)
return false;
}
if (month == 2) { // check for february 29th
var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
if (day>29 || (day==29 && !isleap)) {
alert(“Februari ” + year + ” har inte ” + day + ” dagar!”);
return false;
}
}
return true;
}

function dispDate(dateObj) {

year = dateObj.getYear();
if (year < 2000) year += 1900;

month = dateObj.getMonth()+1;
month = (month < 10) ? “0” + month : month;

day = dateObj.getDate();
day = (day < 10) ? “0” + day : day;

return (year + ” ” + month + ” ” + day);
}

function pregnancyCalc(pregform) {
menstrual = new Date();
ovulation = new Date();
duedate = new Date();
today = new Date();
cycle = 0, luteal = 0;

if (isValidDate(pregform.menstrual.value)) {
menstrualinput = new Date(pregform.menstrual.value);
menstrual.setTime(menstrualinput.getTime())
}
else return false;

cycle = (pregform.cycle.value == “” ? 28 : pregform.cycle.value);

if (pregform.cycle.value != “” && (pregform.cycle.value < 22 || pregform.cycle.value > 45)) {
alert(“Your cycle length is either too short or too long for n”
+ “calculations to be very accurate! We will still try to n”
+ “complete the calculation with the figure you entered. “);
}

luteal = (pregform.luteal.value == “” ? 14 : pregform.luteal.value);

if (pregform.luteal.value != “” && (pregform.luteal.value < 9 || pregform.luteal.value > 16)) {
alert(“Your luteal phase length is either too short or too long for n”
+ “calculations to be very accurate! We will still try to complete n”
+ “the calculation with the figure you entered. “);
}

ovulation.setTime(menstrual.getTime() + (cycle*86400000) – (luteal*86400000));
pregform.conception.value = dispDate(ovulation);

duedate.setTime(ovulation.getTime() + 266*86400000);
pregform.duedate.value = dispDate(duedate);

var fetalage = 14 + 266 – ((duedate – today) / 86400000);
weeks = parseInt(fetalage / 7);
days = Math.floor(fetalage % 7);

fetalage = weeks + “” + (weeks > 1 ? “” : “”) + ” + ” + days + ” dag(ar)”;
pregform.fetalage.value = fetalage;

return false;
}
</script>
</HEAD>
<center>
<form onSubmit=”return pregnancyCalc(this);”>
<table>
<tr><td>
<pre style=”font-family : verdana, arial, sans-serif”>
<br><center><b>1:a dagen i senaste<br>menstruationen:</b><br><input type=text name=menstrual value=”” size=10 maxlength=10><br>(MM/DD/ÅÅÅÅ)<br><br><b>Menscykel:</b><br><input type=text name=cycle value=”” size=2 maxlength=2><br>(normalt 28 dagar)<br><br><b>Lutealfasens längd:</b><br><input type=text name=luteal value=”” size=2 maxlength=2><br>(normalt 14 dagar)<br><br><center><input type=submit value=”Beräkna!”></center><br><b>Befruktningsdatum:</b><br><input type=text name=conception value=”” size=20><br><b>Födelsedatum:</b><br><input type=text name=duedate value=”” size=20><br><b>Du är i vecka:</b><br><input type=text name=fetalage value=”” size=20></center></pre></td></tr></table></form></center><center>Läs mer om beräknat<br><a href=”http://javascriptsource.com”>förlossningsdatum</a>
</center><br>[/CODE]

to post a comment
JavaScript

23 Comments(s)

Copy linkTweet thisAlerts:
@avaOct 28.2005 — Try this function:

[CODE]function newdateformat(olddate)
{
var dates=olddate.split("/");
days = dates[1];
months = dates[0];
years = dates[2];
newdate = years + months + days

alert(newdate)
}[/CODE]
Copy linkTweet thisAlerts:
@VicOct 28.2005 — Try to avoid dd/mm or mm/dd as these will change with the locale of the user (i.e., browser on system with American setting will interpret 2/3/05 as 3 Feb 2005, whereas on system with European setting this would be intepreted as 2 Mar 2005).

Better way is to use dd-MMM-yyyy format, such as 3-Feb-2005.

If your sting is "yyyymmdd" called dateString, you can define the date as:

[CODE]var monthName = {1:"Jan",2:"Feb",3:"Mar",4:"Apr",5:"May",6:"Jun",7:"Jul",8:"Aug",9:"Sep",10:"Oct",11:"Nov",12:"Dec"}

myDate = new Date (dateString.substr(6,2) + "-" + monthName[dateString.substr(4,2)] + "-" + dateString.substr(0,4))[/CODE]
Copy linkTweet thisAlerts:
@VicOct 28.2005 — Change monthName[dateString.substr(4,2)] to monthName[parseInt(dateString.substr(4,2))] in the above code to avoid problem with single-vs-double digit months. (ie 01 = 1)
Copy linkTweet thisAlerts:
@thoeriauthorOct 28.2005 — Try this function:

[CODE]function newdateformat(olddate)
{
var dates=olddate.split("/");
days = dates[1];
months = dates[0];
years = dates[2];
newdate = years + months + days

alert(newdate)
}[/CODE]
[/QUOTE]

Hi,

thanks for anwering, but I a real noob. Where does this code go? Am I suppused to take away parts of the original code?

Cheers,

Thoeri
Copy linkTweet thisAlerts:
@thoeriauthorOct 28.2005 — Try to avoid dd/mm or mm/dd as these will change with the locale of the user (i.e., browser on system with American setting will interpret 2/3/05 as 3 Feb 2005, whereas on system with European setting this would be intepreted as 2 Mar 2005).

Better way is to use dd-MMM-yyyy format, such as 3-Feb-2005.

If your sting is "yyyymmdd" called dateString, you can define the date as:

[CODE]var monthName = {1:"Jan",2:"Feb",3:"Mar",4:"Apr",5:"May",6:"Jun",7:"Jul",8:"Aug",9:"Sep",10:"Oct",11:"Nov",12:"Dec"}

myDate = new Date (dateString.substr(6,2) + "-" + monthName[dateString.substr(4,2)] + "-" + dateString.substr(0,4))[/CODE]
[/QUOTE]

Hi,

it would be ok that people entered the numbers like dd-mm-yyyy if that is better than yyyy-mm-dd. But how to I modify my original code in my first post to make that possible? As I said, I am a REAL noob.

Cheers,

Thoeri
Copy linkTweet thisAlerts:
@konithomimoOct 28.2005 — Place this script in your header:
<i>
</i>&lt;script type="text/javascript"&gt;

function pregnancy(preg)
{
var preglength=preg.length;
var month = preg.charAt(0) + preg.charAt(1);
var day = preg.charAt(3) +preg.charAt(4);
var year = preg.charAt(6) + preg.charAt(7) + preg.charAt(8) + preg.charAt(9);
var m = eval(month);
var d = eval(day);
var y = eval(year);

if (preglength != 10)
{
alert(Input must be as 10 characters in the format mm/dd/yyyy');
}

if (month &lt; 1 || month &gt; 12)
{
alert("Månad måste vara 1 - 12.");
}

if (day &lt; 1 || day &gt; 31)
{
alert("Dag måste vara 1 - 31.");
}

if ((month==4 || month==6 || month==9 || month==11) &amp;&amp; day==31)
{
alert("Månaden "+month+" har inte 31 dagar!")
}

if (month == 2)
{
// check for february 29th
var isleap = (year % 4 == 0 &amp;&amp; (year % 100 != 0 || year % 400 == 0));

if (day&gt;29 || (day==29 &amp;&amp; !isleap))
{
alert("Februari " + year + " har inte " + day + " dagar!");
}
}

else
{
document.getElementById('pregDate').value = year + month + day;
}
}
&lt;/script&gt;


And then call it anyway you want. For example, this is how you call it by using a textbox:
<i>
</i>Pregnancy Date(mm/dd/yyyy):
&lt;input type="text" id="pregDate" length="10" onkeyup="this.value=this.value.replace(/D/|///,'')" onblur="pregnancy(this.value)"&gt;


Once you tab away from the textbox it runs the necessary script.

The above will take in a date in the form of mm/dd/yyyy and turn it into yyyymmdd.
Copy linkTweet thisAlerts:
@konithomimoOct 28.2005 — Here is how your entire code should look with the code from my last post:
<i>
</i>&lt;html&gt;
&lt;script type="text/javascript"&gt;

function pregnancy(preg)
{
var preglength=preg.length;
var month = preg.charAt(0) + preg.charAt(1);
var day = preg.charAt(3) +preg.charAt(4);
var year = preg.charAt(6) + preg.charAt(7) + preg.charAt(8) + preg.charAt(9);
var m = eval(month);
var d = eval(day);
var y = eval(year);

if (preglength != 10)
{
alert(Input must be as 10 characters in the format mm/dd/yyyy');
}

if (month &lt; 1 || month &gt; 12)
{
alert("Månad måste vara 1 - 12.");
}

if (day &lt; 1 || day &gt; 31)
{
alert("Dag måste vara 1 - 31.");
}

if ((month==4 || month==6 || month==9 || month==11) &amp;&amp; day==31)
{
alert("Månaden "+month+" har inte 31 dagar!")
}

if (month == 2)
{
// check for february 29th
var isleap = (year % 4 == 0 &amp;&amp; (year % 100 != 0 || year % 400 == 0));

if (day&gt;29 || (day==29 &amp;&amp; !isleap))
{
alert("Februari " + year + " har inte " + day + " dagar!");
}
}

else
{
document.getElementById('pregDate').value = year + month + day;
}
}

function pregnancyCalc(pregform) {
menstrual = new Date();
ovulation = new Date();
duedate = new Date();
today = new Date();
cycle = 0, luteal = 0;

if (isValidDate(pregform.menstrual.value)) {
menstrualinput = new Date(pregform.menstrual.value);
menstrual.setTime(menstrualinput.getTime())
}
else return false;

cycle = (pregform.cycle.value == "" ? 28 : pregform.cycle.value);

if (pregform.cycle.value != "" &amp;&amp; (pregform.cycle.value &lt; 22 || pregform.cycle.value &gt; 45)) {
alert("Your cycle length is either too short or too long for n"
+ "calculations to be very accurate! We will still try to n"
+ "complete the calculation with the figure you entered. ");
}

luteal = (pregform.luteal.value == "" ? 14 : pregform.luteal.value);

if (pregform.luteal.value != "" &amp;&amp; (pregform.luteal.value &lt; 9 || pregform.luteal.value &gt; 16)) {
alert("Your luteal phase length is either too short or too long for n"
+ "calculations to be very accurate! We will still try to complete n"
+ "the calculation with the figure you entered. ");
}


ovulation.setTime(menstrual.getTime() + (cycle*86400000) - (luteal*86400000));
pregform.conception.value = dispDate(ovulation);


duedate.setTime(ovulation.getTime() + 266*86400000);
pregform.duedate.value = dispDate(duedate);


var fetalage = 14 + 266 - ((duedate - today) / 86400000);
weeks = parseInt(fetalage / 7);
days = Math.floor(fetalage % 7);


fetalage = weeks + "" + (weeks &gt; 1 ? "" : "") + " + " + days + " dag(ar)";
pregform.fetalage.value = fetalage;

return false;
}
&lt;/script&gt;
&lt;/HEAD&gt;
&lt;body&gt;
&lt;center&gt;
&lt;form onSubmit="return pregnancyCalc(this);"&gt;
&lt;table&gt;
&lt;tr&gt;&lt;td&gt;
&lt;pre style="font-family : verdana, arial, sans-serif"&gt;
&lt;br&gt;
&lt;center&gt;
&lt;b&gt;1:a dagen i senaste&lt;br&gt;menstruationen:&lt;/b&gt;
&lt;br&gt;
&lt;input type="text" name="menstrual" id="pregDate" value="" size="10" maxlength="10" onkeyup="this.value=this.value.replace(/D/|///,'')" onblur="pregnancy(this.value)"&gt;
&lt;br&gt;
(MM/DD/ÅÅÅÅ)
&lt;br&gt;
&lt;br&gt;
&lt;b&gt;Menscykel:&lt;/b&gt;
&lt;br&gt;&lt;input type=text name=cycle value="" size=2 maxlength=2&gt;
&lt;br&gt;
(normalt 28 dagar)&lt;br&gt;
&lt;br&gt;
&lt;b&gt;Lutealfasens längd:&lt;/b&gt;
&lt;br&gt;
&lt;input type=text name=luteal value="" size=2 maxlength=2&gt;&lt;br&gt;(normalt 14 dagar)
&lt;br&gt;
&lt;br&gt;
&lt;center&gt;
&lt;input type=submit value="Beräkna!"&gt;
&lt;/center&gt;
&lt;br&gt;
&lt;b&gt;Befruktningsdatum:&lt;/b&gt;
&lt;br&gt;
&lt;input type=text name=conception value="" size=20&gt;
&lt;br&gt;
&lt;b&gt;Födelsedatum:&lt;/b&gt;
&lt;br&gt;
&lt;input type=text name=duedate value="" size=20&gt;
&lt;br&gt;
&lt;b&gt;Du är i vecka:&lt;/b&gt;
&lt;br&gt;
&lt;input type=text name=fetalage value="" size=20&gt;
&lt;/center&gt;
&lt;/pre&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;/form&gt;
&lt;/center&gt;
&lt;center&gt;Läs mer om beräknat&lt;br&gt;
&lt;a href="http://javascriptsource.com"&gt;förlossningsdatum&lt;/a&gt;
&lt;/center&gt;
&lt;br&gt;
&lt;/body&gt;
&lt;/html&gt;


That will change the text in the input box from mm/dd/yyyy to yyyymmdd.
Copy linkTweet thisAlerts:
@thoeriauthorOct 28.2005 — Hi thanks for answeing,

but I want the vistor to enter it in yyyymmdd!

I have a script, where people enter the date in mmddyyyy. But this I don't want. I jsut want to modify that. There is a textbox where the script is supposed to be entered. But in yyyymmdd.

Tanks,

Thoeri
Copy linkTweet thisAlerts:
@thoeriauthorOct 28.2005 — Hi again,

it gives me:

Fatal error: Call to undefined function: set() in /home/barntota/public_html/portal/components/com_content/content.php on line 198

Cheers,

Thoeri
Copy linkTweet thisAlerts:
@konithomimoOct 28.2005 — I forgot to add in the opening header tag. Here is the code it:
<i>
</i>
&lt;html&gt;
&lt;head&gt;
&lt;script type="text/javascript"&gt;

function pregnancy(preg)
{
var preglength=preg.length;
var month = preg.charAt(0) + preg.charAt(1);
var day = preg.charAt(3) +preg.charAt(4);
var year = preg.charAt(6) + preg.charAt(7) + preg.charAt(8) + preg.charAt(9);
var m = eval(month);
var d = eval(day);
var y = eval(year);

if (preglength != 10)
{
alert(Input must be as 10 characters in the format mm/dd/yyyy');
}

if (month &lt; 1 || month &gt; 12)
{
alert("Månad måste vara 1 - 12.");
}

if (day &lt; 1 || day &gt; 31)
{
alert("Dag måste vara 1 - 31.");
}

if ((month==4 || month==6 || month==9 || month==11) &amp;&amp; day==31)
{
alert("Månaden "+month+" har inte 31 dagar!")
}

if (month == 2)
{
// check for february 29th
var isleap = (year % 4 == 0 &amp;&amp; (year % 100 != 0 || year % 400 == 0));

if (day&gt;29 || (day==29 &amp;&amp; !isleap))
{
alert("Februari " + year + " har inte " + day + " dagar!");
}
}

else
{
document.getElementById('pregDate').value = year + month + day;
}
}

function pregnancyCalc(pregform) {
menstrual = new Date();
ovulation = new Date();
duedate = new Date();
today = new Date();
cycle = 0, luteal = 0;

if (isValidDate(pregform.menstrual.value)) {
menstrualinput = new Date(pregform.menstrual.value);
menstrual.setTime(menstrualinput.getTime())
}
else return false;

cycle = (pregform.cycle.value == "" ? 28 : pregform.cycle.value);

if (pregform.cycle.value != "" &amp;&amp; (pregform.cycle.value &lt; 22 || pregform.cycle.value &gt; 45)) {
alert("Your cycle length is either too short or too long for n"
+ "calculations to be very accurate! We will still try to n"
+ "complete the calculation with the figure you entered. ");
}

luteal = (pregform.luteal.value == "" ? 14 : pregform.luteal.value);

if (pregform.luteal.value != "" &amp;&amp; (pregform.luteal.value &lt; 9 || pregform.luteal.value &gt; 16)) {
alert("Your luteal phase length is either too short or too long for n"
+ "calculations to be very accurate! We will still try to complete n"
+ "the calculation with the figure you entered. ");
}


ovulation.setTime(menstrual.getTime() + (cycle*86400000) - (luteal*86400000));
pregform.conception.value = dispDate(ovulation);


duedate.setTime(ovulation.getTime() + 266*86400000);
pregform.duedate.value = dispDate(duedate);


var fetalage = 14 + 266 - ((duedate - today) / 86400000);
weeks = parseInt(fetalage / 7);
days = Math.floor(fetalage % 7);


fetalage = weeks + "" + (weeks &gt; 1 ? "" : "") + " + " + days + " dag(ar)";
pregform.fetalage.value = fetalage;

return false;
}
&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;center&gt;
&lt;form onSubmit="return pregnancyCalc(this);"&gt;
&lt;table&gt;
&lt;tr&gt;&lt;td&gt;
&lt;pre style="font-family : verdana, arial, sans-serif"&gt;
&lt;br&gt;
&lt;center&gt;
&lt;b&gt;1:a dagen i senaste&lt;br&gt;menstruationen:&lt;/b&gt;
&lt;br&gt;
&lt;input type="text" name="menstrual" id="pregDate" value="" size="10" maxlength="10" onkeyup="this.value=this.value.replace(/D/|///,'')" onblur="pregnancy(this.value)"&gt;
&lt;br&gt;
(MM/DD/ÅÅÅÅ)
&lt;br&gt;
&lt;br&gt;
&lt;b&gt;Menscykel:&lt;/b&gt;
&lt;br&gt;&lt;input type=text name=cycle value="" size=2 maxlength=2&gt;
&lt;br&gt;
(normalt 28 dagar)&lt;br&gt;
&lt;br&gt;
&lt;b&gt;Lutealfasens längd:&lt;/b&gt;
&lt;br&gt;
&lt;input type=text name=luteal value="" size=2 maxlength=2&gt;&lt;br&gt;(normalt 14 dagar)
&lt;br&gt;
&lt;br&gt;
&lt;center&gt;
&lt;input type=submit value="Beräkna!"&gt;
&lt;/center&gt;
&lt;br&gt;
&lt;b&gt;Befruktningsdatum:&lt;/b&gt;
&lt;br&gt;
&lt;input type=text name=conception value="" size=20&gt;
&lt;br&gt;
&lt;b&gt;Födelsedatum:&lt;/b&gt;
&lt;br&gt;
&lt;input type=text name=duedate value="" size=20&gt;
&lt;br&gt;
&lt;b&gt;Du är i vecka:&lt;/b&gt;
&lt;br&gt;
&lt;input type=text name=fetalage value="" size=20&gt;
&lt;/center&gt;
&lt;/pre&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;/form&gt;
&lt;/center&gt;
&lt;center&gt;Läs mer om beräknat&lt;br&gt;
&lt;a href="http://javascriptsource.com"&gt;förlossningsdatum&lt;/a&gt;
&lt;/center&gt;
&lt;br&gt;
&lt;/body&gt;
&lt;/html&gt;
Copy linkTweet thisAlerts:
@thoeriauthorOct 28.2005 — Hi,

I am REALLY happy that you are trying to help me. But unfortunately the script doesn't work. Can't enter zeros and when executing the calculation I get a fatal error. Have no idea why. This is the link to the original script with a demo if you would like to have a look. I want it to be the same except that the date should be entered in YYYYMMDD or YYYY-MM-DD and the calculated result shown in the same format. Any ideas? Plus a big, big thank you to you for your efforts and patience with me.

Regards,

Thoeri
Copy linkTweet thisAlerts:
@konithomimoOct 28.2005 — Please include the link (since you forgot to), and I will take a look at it later. Unfortunately I am at work and am only looking at the code for a few seconds at a time. I will look at it in depth when I get home in a few hours.
Copy linkTweet thisAlerts:
@thoeriauthorOct 28.2005 — Hi,

here is the link:

http://javascript.internet.com/calculators/pregnancy.html

Thanks a lot,

Thoeri
Copy linkTweet thisAlerts:
@thoeriauthorOct 29.2005 — Hi,

any progress? I saw you were online so I better ask. I did not crack it myself yet.

Cheers,

Thomas
Copy linkTweet thisAlerts:
@konithomimoOct 29.2005 — It is almost done.
Copy linkTweet thisAlerts:
@konithomimoOct 29.2005 — Here it is:
<i>
</i>&lt;html&gt;
&lt;head&gt;
&lt;script type="text/javascript"&gt;

function isValidDate(dateStr) {
var datePat = /^(d{4})(d{2})(d{2})$/; // requires 4 digit year

var matchArray = dateStr.match(datePat); // is the format ok?
if (matchArray == null) {
alert("Date is not in a valid format.")
return false;
}
year = eval(matchArray[1]);
month = eval(matchArray[2]); // parse date into variables
day = eval(matchArray[3]);

if (month &lt; 1 || month &gt; 12) { // check month range
alert("Month must be between 1 and 12.");
return false;
}
if (day &lt; 1 || day &gt; 31) {
alert("Day must be between 1 and 31.");
return false;
}
if ((month==4 || month==6 || month==9 || month==11) &amp;&amp; day==31) {
alert("Month "+month+" doesn't have 31 days!")
return false;
}
if (month == 2) { // check for february 29th
var isleap = (year % 4 == 0 &amp;&amp; (year % 100 != 0 || year % 400 == 0));
if (day&gt;29 || (day==29 &amp;&amp; !isleap)) {
alert("February " + year + " doesn't have " + day + " days!");
return false;
}
}
return true;
}

function dispDate(dateObj) {
month = dateObj.getMonth()+1;
month = (month &lt; 10) ? "0" + month : month;

day = dateObj.getDate();
day = (day &lt; 10) ? "0" + day : day;

year = dateObj.getYear();
if (year &lt; 2000) year += 1900;

return (month + "/" + day + "/" + year);
}


function pregnancyCalc(preg) {

var y = preg.charAt(0) + preg.charAt(1) + preg.charAt(2) + preg.charAt(3);
var m = preg.charAt(4) + preg.charAt(5);
var d = preg.charAt(6) + preg.charAt(7);

var ya = eval(y);
var mon = eval(m) - 1;
var da = eval(d);
var menstrual = new Date(y,mon,d); // creates new date objects

if (isValidDate(pregform.menstrual.value)) { // Validates menstual date
}

else{
return false;
}


var cycles=28;
var luteals=14;


if ((pregform.cycle.value != "") &amp;&amp; ((pregform.cycle.value &lt; 22) || (pregform.cycle.value &gt; 45)))
{
alert("Your cycle length is either too short or too long for n"
+ "calculations to be very accurate! We will still try to n"
+ "complete the calculation with the figure you entered. ");
}


cycles = eval(document.getElementById('cycle').value);


if (pregform.luteal.value != "" &amp;&amp; (pregform.luteal.value &lt; 9 || pregform.luteal.value &gt; 16)) {
alert("Your luteal phase length is either too short or too long for n"
+ "calculations to be very accurate! We will still try to complete n"
+ "the calculation with the figure you entered. ");
}


luteals = eval(document.getElementById('luteal').value);

// sets ovulation date to menstrual date + cycle days - luteal days
var thisday = da + cycles - luteals;

var ovulation = new Date(ya,mon,thisday);
pregform.conception.value = dispDate(ovulation);

// sets due date to ovulation date plus 266 days
var duedate = new Date(ya,mon,(thisday+266));
pregform.duedate.value = dispDate(duedate);

// sets fetal age to 14 + 266 (pregnancy time) - time left
var today = new Date();
var myday = eval(duedate.getTime() - today.getTime());
var fetalage = 14 + 266 - (myday / 86400000);
weeks = parseInt(fetalage / 7); // sets weeks to whole number of weeks
days = Math.floor(fetalage % 7); // sets days to the whole number remainder

// fetal age message, automatically includes 's' on week and day if necessary
fetalage = weeks + " week" + (weeks &gt; 1 ? "s" : "") + ", " + days + " days";
pregform.fetalage.value = fetalage;



return false; // form should never submit, returns false


}


&lt;/script&gt;
&lt;/HEAD&gt;

&lt;BODY&gt;

&lt;center&gt;
&lt;form name="pregform"&gt;
&lt;table&gt;
&lt;tr&gt;&lt;td&gt;
&lt;pre&gt;
Last Menstrual Period: &lt;input type="text" name="menstrual" id="menstrual" value="" size=10 maxlength=10&gt;
(YYYYMMDD format)

Average Length of Cycles: &lt;input type="text" name="cycle" id="cycle" value="" size=3 maxlength=3&gt; (22 to 45)
(defaults to 28)

Average Luteal Phase Length: &lt;input type="text" name="luteal" id="luteal" value="" size=3 maxlength=3&gt; (9 to 16)
(defaults to 14)

&lt;center&gt;&lt;input type=button value="Calculate!" onClick="pregnancyCalc(document.getElementById('menstrual').value)"&gt;&lt;/center&gt;

Estimated Conception: &lt;input type=text name=conception value="" size=20&gt;
Estimated Due Date: &lt;input type=text name=duedate value="" size=20&gt;
Estimated Fetal Age: &lt;input type=text name=fetalage value="" size=20&gt;
&lt;/pre&gt;
&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;
&lt;/form&gt;
&lt;/center&gt;
&lt;/body&gt;
&lt;/html&gt;


EDIT -

The above will take in the date as: YYYYMMDD and display the results as MM/DD/YYYY.

It should work perfectly.
Copy linkTweet thisAlerts:
@thoeriauthorOct 30.2005 — HI again!


Thanks for the new script! But something is not correct the reslut shows up as:

Estimates conception: NaN/NaN/NaN

Estimated due date: NaN/NaN/NaN

Estimated fetal age: NaN weeks, NaN dayd


What could be wrong?

Regards,

Thoeri
Copy linkTweet thisAlerts:
@konithomimoOct 30.2005 — It works. Look at the following link. I put it up online to test it, and it works perfectly.

[URL=http://aorebirth.ao.funpic.org/TestPages/testpage.html]Pregnancy Calculator[/URL]
Copy linkTweet thisAlerts:
@thoeriauthorOct 30.2005 — Sorry,

but it doesn't work on your site either, not for me. I still get the same result, "NaN". Maybe there is a browser issue. I use Internet Explorer 6 in Swedish and FireFox 1.06 in Swedish. Maybe the swedish browser interprets the date different? I have no idea? Do you?

Cheers,

Thoeri
Copy linkTweet thisAlerts:
@konithomimoOct 30.2005 — I was getting NaN at first before I gave you any code, so I updated it and then posted it. It displays perfectly for me. Try this one:
<i>
</i>&lt;html&gt;
&lt;head&gt;
&lt;script type="text/javascript"&gt;

function isValidDate(dateStr) {
var datePat = /^(d{4})(d{2})(d{2})$/; // requires 4 digit year

var matchArray = dateStr.match(datePat); // is the format ok?
if (matchArray == null) {
alert("Date is not in a valid format.")
return false;
}
year = eval(matchArray[1]);
month = eval(matchArray[2]); // parse date into variables
day = eval(matchArray[3]);

if (month &lt; 1 || month &gt; 12) { // check month range
alert("Month must be between 1 and 12.");
return false;
}
if (day &lt; 1 || day &gt; 31) {
alert("Day must be between 1 and 31.");
return false;
}
if ((month==4 || month==6 || month==9 || month==11) &amp;&amp; day==31) {
alert("Month "+month+" doesn't have 31 days!")
return false;
}
if (month == 2) { // check for february 29th
var isleap = (year % 4 == 0 &amp;&amp; (year % 100 != 0 || year % 400 == 0));
if (day&gt;29 || (day==29 &amp;&amp; !isleap)) {
alert("February " + year + " doesn't have " + day + " days!");
return false;
}
}
return true;
}



function pregnancyCalc(preg) {

var y = preg.charAt(0) + preg.charAt(1) + preg.charAt(2) + preg.charAt(3);
var m = preg.charAt(4) + preg.charAt(5);
var d = preg.charAt(6) + preg.charAt(7);

var ya = eval(y);
var mon = eval(m) - 1;
var da = eval(d);
var menstrual = new Date(y,mon,d); // creates new date objects

if (isValidDate(pregform.menstrual.value)) { // Validates menstual date
}

else{
return false;
}


var cycles=28;
var luteals=14;


if ((pregform.cycle.value != "") &amp;&amp; ((pregform.cycle.value &lt; 22) || (pregform.cycle.value &gt; 45)))
{
alert("Your cycle length is either too short or too long for n"
+ "calculations to be very accurate! We will still try to n"
+ "complete the calculation with the figure you entered. ");
}


cycles = eval(document.getElementById('cycle').value);


if (pregform.luteal.value != "" &amp;&amp; (pregform.luteal.value &lt; 9 || pregform.luteal.value &gt; 16)) {
alert("Your luteal phase length is either too short or too long for n"
+ "calculations to be very accurate! We will still try to complete n"
+ "the calculation with the figure you entered. ");
}


luteals = eval(document.getElementById('luteal').value);

// sets ovulation date to menstrual date + cycle days - luteal days
var thisday = da + cycles - luteals;

var ovulation = new Date(ya,mon,thisday);
pregform.conception.value = ovulation;

// sets due date to ovulation date plus 266 days
var duedate = new Date(ya,mon,(thisday+266));
pregform.duedate.value = duedate;

// sets fetal age to 14 + 266 (pregnancy time) - time left
var today = new Date();
var myday = eval(duedate.getTime() - today.getTime());
var fetalage = 14 + 266 - (myday / 86400000);
weeks = parseInt(fetalage / 7); // sets weeks to whole number of weeks
days = Math.floor(fetalage % 7); // sets days to the whole number remainder

// fetal age message, automatically includes 's' on week and day if necessary
fetalage = weeks + " week" + (weeks &gt; 1 ? "s" : "") + ", " + days + " days";
pregform.fetalage.value = fetalage;



return false; // form should never submit, returns false


}


&lt;/script&gt;
&lt;/HEAD&gt;

&lt;BODY&gt;

&lt;center&gt;
&lt;form name="pregform"&gt;
&lt;table&gt;
&lt;tr&gt;&lt;td&gt;
&lt;pre&gt;
Last Menstrual Period: &lt;input type="text" name="menstrual" id="menstrual" value="" size=10 maxlength=10&gt;
(YYYYMMDD format)

Average Length of Cycles: &lt;input type="text" name="cycle" id="cycle" value="" size=3 maxlength=3&gt; (22 to 45)
(defaults to 28)

Average Luteal Phase Length: &lt;input type="text" name="luteal" id="luteal" value="" size=3 maxlength=3&gt; (9 to 16)
(defaults to 14)

&lt;center&gt;&lt;input type=button value="Calculate!" onClick="pregnancyCalc(document.getElementById('menstrual').value)"&gt;&lt;/center&gt;

Estimated Conception: &lt;input type=text name=conception value="" size=20&gt;
Estimated Due Date: &lt;input type=text name=duedate value="" size=20&gt;
Estimated Fetal Age: &lt;input type=text name=fetalage value="" size=20&gt;
&lt;/pre&gt;
&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;
&lt;/form&gt;
&lt;/center&gt;
&lt;/body&gt;
&lt;/html&gt;


If that doesnt work then it might just be the way that you display dates. I have other ways of doing this, so if it doesnt work then i can try it another way.
Copy linkTweet thisAlerts:
@thoeriauthorOct 30.2005 — Hi again,

nope it's still not working corrrectly. But the result now is slightly different. Before it was NaN/NaN/NaN. Now it's only NaN. It it' not possible to solve it like this other date formats might me OK. Like YYYY-MM-DD or DDMMYYYY or DD-MM-YYYY. This was much more difficult than I anticipated. ?

I would be happy if you gave it another go.

Thanks a bunch,

Thoeri
Copy linkTweet thisAlerts:
@thoeriauthorOct 31.2005 — Hi again,

just want to tell you that I solved it, in case you were working on it. Your help was fantastic and thank to that I solved it.

Keep up the good work ?
Copy linkTweet thisAlerts:
@konithomimoOct 31.2005 — You're welcome. Good luck with the rest of it.
×

Success!

Help @thoeri spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.24,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...