Programming Tutorials Browser Tutorials Articles Struts Tutorials Hibernate Tutorials

  Tutorial: Fill an input field with the current date and time with javascript

Save time for your self, and let javascript give you the current time.

Tutorial Details:

We have a customer that enters lots of articles through a form, and one of the fields are a date/time when the auction is to start. To speed up their work I created a handy function that fills the input field with the current date/time.

Lets set the id of the input field to "startWhen", by writing this:
This way we can access that field with this function: document.getElementById("startWhen")
Let's assign the result from that function as the variable "theInput".
The variable has a number of different attributes and the most important for us right now is .value, which holds the text that is displayed in the field.

Ok, let's look at the "action... the javascript function: "setStartTimeNow()".
Basicly we get a "date-object" like this: var d = new Date();
That object have lots of functions for getting the different variables like year, month, hour etc.
Important for us (that made me rub my head a little...) is that the function getMonth() returns a number between 0 and 11, where 0 is january and 11 is december.
So I add 1 to the the month, so we get the "normal" way to write month...

Because I live in Sweden I write the date/time like this: 2006-06-04 23:19.
Feel free to change around with the formatting if you need to.

There is some more "magic" to get the format good looking.
Numbers less than 10 are returned without a leading zero, but I like to have a leading zero for the format.
So this short way to write a if makes sure that numbers lover than 10 get's a zero infront of the number: (currMonth < 10 ? "0" : "")

I now create a link that activates the javascript function: Set current time
It's called by using the "onclick" event.


 

Read Tutorial at: Click here to view the tutorial

Rate Tutorial:
Fill an input field with the current date and time with javascript

View Tutorial:
Fill an input field with the current date and time with javascript

Related Tutorials:

Integrating Databases
Integrating Databases
 
Write high-performance RMI servers and Swing clients - JavaWorld - April 1999
Write high-performance RMI servers and Swing clients - JavaWorld - April 1999
 
Building a Java servlet framework using reflection, Part 2 - JavaWorld February 2000
Building a Java servlet framework using reflection, Part 2 - JavaWorld February 2000
 
Add XML to your J2EE applications - JavaWorld February 2001
Integrate an XML presentation layer in the J2EE layered architecture
 
Jato: The new kid on the open source block, Part 3 - JavaWorld May 2001
Jato: The new kid on the open source block, Part 3 - JavaWorld May 2001
 
Clean up your wire protocol with SOAP, Part 3 - JavaWorld June 2001
Clean up your wire protocol with SOAP, Part 3 - JavaWorld June 2001
 
Access the world's biggest database with Web DataBase Connectivity - JavaWorld March 2001
Access the world's biggest database with Web DataBase Connectivity - JavaWorld March 2001
 
Matchmaking with regular expressions - JavaWorld July 2001
Matchmaking with regular expressions - JavaWorld July 2001
 
Facilitate form
Facilitate form processing with the Form Processing API
 
Pushlets: Send events from servlets to DHTML client browsers
Pushlets: Send events from servlets to DHTML client browsers
 
Working in Java time
Working in Java time
 
XSLT blooms with Java
XSLT blooms with Java
 
Master the Jxta shell, Part 1
Master the Jxta shell, Part 1
 
Transform data into Web applications with Cocoon
Transform data into Web applications with Cocoon
 
Nice widget
Nice widget
 
A Generic MVC Model in Java
A Generic MVC Model in Java Model-View-Controller (MVC) is a widely used design pattern, especially popular in graphical user interface (GUI) programming. JDK 1.5 introduces parameterized types, or generics. Combining the two allows for a generic imple
 
JSP date example
JSP date example JSP Date Example Till now you learned about the JSP syntax, now I will show you how to create a simple dynamic JSP page that prints the current date and time. So the following code accomplish this: <%@page contentType="text/html"
 
Submit a Request for a Project to be done
Submit a Request for a Project to be done Fill this form to Submit a Request for a Project to be done. User's Personal Details Name : Address: Email: Project Name: Project Description Language to be used: Delivery Date:
 
Client Side Address Validation in Struts
Client Side Address Validation in Struts Client Side Address Validation in Struts In this lesson we will create JSP page for entering the address and use the functionality provided by Validator Framework to validate the user data on the browser.
 
Struts Validator Framework Tutorial with Example
Struts Validator Framework Tutorial with Example Struts HTML Tags Struts Validator Framework This lesson introduces you the Struts Validator Framework. In this lesson you will learn how to use Struts Validator Framework to validate the user
 
Site navigation
 

 

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2006. All rights reserved.