LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   html form current datetime (https://www.linuxquestions.org/questions/programming-9/html-form-current-datetime-55153/)

meluser 04-15-2003 11:55 AM

html form current datetime
 
hi,

i have a form that has a field, that i would like to set the field to the current date and time in the context of
0000-00-00 00:00:00

could someone help.

Cheers,
Mel

david_ross 04-15-2003 01:38 PM

If you want to use javascript then use quick one I just wrote:
Code:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<TITLE>Current Date and Time</TITLE>
</HEAD>
<BODY>
<FORM name="form1"><INPUT name="box1" type="text"></FORM>

<SCRIPT type="text/javascript" language="JavaScript">

function update(){
time = new Date();
year = (time.getYear() + 1900);
month = (time.getMonth() + 1);
date = time.getDate();
hours = time.getHours();
mins = time.getMinutes();
secs = time.getSeconds();
if(mins.length<2){mins = "0"+secs}
if(secs.length<2){secs = "0"+secs}
if(month.length<2){month = "0"+secs}
if(date.length<2){date  = "0"+secs}


// Config Starts
format = year+"-"+month+"-"+date+" "+hours+":"+mins+":"+secs;
document.form1.box1.value = format;
// Config Ends
}
setInterval("update()",1000);
</SCRIPT>
</BODY>
</HTML>


If you have SSI you can use that too.

meluser 04-15-2003 03:26 PM

thanks for the script.

what i am trying to do, is to display the current date and time only. not live date and time, so the user will be able to adjust it. with the nice script you sent me we cant adjust it!!!

Cheers,

david_ross 04-16-2003 12:25 PM

Use this then:
Code:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<TITLE>Current Date and Time</TITLE>
</HEAD>
<BODY>
<FORM name="form1"><INPUT name="box1" type="text"></FORM>

<SCRIPT type="text/javascript" language="JavaScript">

time = new Date();
year = (time.getYear() + 1900);
month = (time.getMonth() + 1);
date = time.getDate();
hours = time.getHours();
mins = time.getMinutes();
secs = time.getSeconds();
if(mins.length<2){mins = "0"+secs}
if(secs.length<2){secs = "0"+secs}
if(month.length<2){month = "0"+secs}
if(date.length<2){date  = "0"+secs}


// Config Starts
format = year+"-"+month+"-"+date+" "+hours+":"+mins+":"+secs;
document.form1.box1.value = format;
// Config Ends

</SCRIPT>
</BODY>
</HTML>

If you wand static text and have SSI I would reccomend using that as it will be compatible with any browser.

meluser 04-16-2003 01:53 PM

thanks very much.

the display i get is showing the year wrong!!

3903-4-16 19:48:34


another question. i would like to have another field within the frame, but with the current time 10 minutes ago!!!. so with the above it would display

2003-4-16 19:38:34

thanks one again

meluser 04-16-2003 02:02 PM

another question. could i possibly display the results in the format 0000-00-00 00:00:00

cheers

david_ross 04-16-2003 02:46 PM

I actually made some typos - this should work:
Code:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<TITLE>Current Date and Time</TITLE>
</HEAD>
<BODY>
<FORM name="form1"><INPUT name="box1" type="text"><INPUT name="box2" type="text"></FORM>

<SCRIPT type="text/javascript" language="JavaScript">

time = new Date();
year = new String((time.getYear() + 1900));
month = new String((time.getMonth() + 1));
date = new String(time.getDate());
hours = new String(time.getHours());
mins = new String(time.getMinutes());
secs = new String(time.getSeconds());
if(mins.length<2){mins = "0"+mins}
if(secs.length<2){secs = "0"+secs}
if(month.length<2){month = "0"+month}
if(date.length<2){date  = "0"+date}


// Config Starts
box1format = year+"-"+month+"-"+date+" "+hours+":"+mins+":"+secs;
box2format = year+"-"+month+"-"+date+" "+hours+":"+(mins-10)+":"+secs;
document.form1.box1.value = box1format;
document.form1.box2.value = box2format;
// Config Ends

</SCRIPT>
</BODY>
</HTML>


meluser 04-16-2003 02:53 PM

excellent results, thanks.

the year is still not right
3903-04-16 20:52:35
3903-04-16 20:42:35

david_ross 04-16-2003 02:56 PM

It is fine for me - what browser are you using - I have had this problem before.

david_ross 04-16-2003 02:57 PM

I jusst tried it in IE and I see what you mean.

meluser 04-16-2003 02:59 PM

Hi,

i have a way to solve it till 2004. by removing the get year. but i dont think this is good!!


thanks for the help. i have another question. is it possible to implement this in a pop up calendar!!!! date, and time

Chers,

david_ross 04-16-2003 03:04 PM

This seems OK:
Code:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<TITLE>Current Date and Time</TITLE>
</HEAD>
<BODY>
<FORM name="form1"><INPUT name="box1" type="text"><INPUT name="box2" type="text"></FORM>

<SCRIPT type="text/javascript" language="JavaScript">

time = new Date();
year = new String(time.getYear());
if(navigator.appName != "Microsoft Internet Explorer"){year = (year*1)+1900}
month = new String((time.getMonth() + 1));
date = new String(time.getDate());
hours = new String(time.getHours());
mins = new String(time.getMinutes());
secs = new String(time.getSeconds());
if(mins.length<2){mins = "0"+mins}
if(secs.length<2){secs = "0"+secs}
if(month.length<2){month = "0"+month}
if(date.length<2){date  = "0"+date}


// Config Starts
box1format = year+"-"+month+"-"+date+" "+hours+":"+mins+":"+secs;
box2format = year+"-"+month+"-"+date+" "+hours+":"+(mins-10)+":"+secs;
document.form1.box1.value = box1format;
document.form1.box2.value = box2format;
// Config Ends

</SCRIPT>
</BODY>
</HTML>


david_ross 04-16-2003 03:06 PM

I'm not sure what you mean by in a popup calendar - the chances are yes, but I ain't gonna write it!

meluser 04-16-2003 03:11 PM

another problem

3903-04-16 21:09:44
3903-04-16 21:-1:44

meluser 04-16-2003 03:16 PM

thanks i got 2003 working.

this is some results.
2003-04-16 21:5:01
2003-04-16 21:15:01

is there anyway to display 21:5:01 as 21:05:01

cheers


All times are GMT -5. The time now is 05:01 PM.