Difference between datetime and datetime-local in HTML5


The datetime-local input is different from the other one in that it excludes the time zone. Use datetime-local if your application doesn't care about the time zone. The datetime input type is still catching up with some browsers.

Let’s look into further for getting better understanding between datetime and datetime-local in HTML5

HTML5 datetime

The machine-readable date and time of the <time> element is defined by the HTML <time> datetime Attribute. The time and date are entered in the following format: YYYY-MM-DDThh:mm:ssTZD.

Syntax

Following is the syntax for HTML datetime

<time datetime="YYYY-MM-DDThh:mm:ssTZD"> 

Let’s look into the following example

Example

In the following example we are using the HTML <time>datetime attribute.

<!DOCTYPE html>
<html>
    <body style="text-align:center;">
        <h1>HTML dateTime Attribute</h1>
        <p>Gandhi Jayathi
        <time datetime="1869--10-02 12:00">
            was declared as a dry day.
		</time>
	</p>
	</body>
</html>

Output

When the script gets executed, it will generate an output consisting of the text mentioned with the HTML datetime attribute and display it on the webpage.

Example

Considering the another example for datetime attribute.

<!DOCTYPE HTML>
<html>
   <body>
      <form action = "/cgi-bin/html5.cgi" method = "get">
         Date and Time : <input type = "datetime" name = "newinput" />
         <input type = "submit" value = "submit" />
      </form>
   </body>
</html> 

Output

On running the above script, the output window will pop up and display the input field for the user to enter the date and time mentioned with the datetime attribute on the webpage.

HTML5 datetime-local

Elements of type <input> With the help of datetime-local, users can quickly enter a date and time, as well as the year, month, and day, as well as the time in hours and minutes.

Syntax

Following is the syntax for datetime-local.

<input type="datetime-local">

Let’s look into the following examples.

Example

In the following we are using the datetime-local for input type.

<!DOCTYPE html>
<html>
    <body>
        <form>
            <label for="birthdaytime">DOB:</label>
            <input type="datetime-local" id="birthdaytime" name="birthdaytime">
            <input type="submit">
        </form>
    </body>
</html>

Output

When the script gets executed, it will generate an output consisting of an input field allowing the user to enter the date of birth mentioned with datetime-local on the webpage.

Example

Let’s look into the another example of usage of datetime-local.

<!DOCTYPE HTML>
<html>
   <body>
      <form action = "/cgi-bin/html5.cgi" method = "get">
         Local Date and Time : <input type = "datetime-local" name = "newinput" />
         <input type = "submit" value = "submit" />
      </form>
   </body>
</html>

Output

On running the above script, it will generate an output making the user enter the input by providing the input field mentioned with datetime-local on the webpage.

Updated on: 11-Oct-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements