Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
HTML DOM Input DatetimeLocal step Property
The HTML DOM Input DatetimeLocal step property sets or returns the value of the step attribute of a datetime-local input field. This property determines the legal number intervals for the seconds (or milliseconds) component of the datetime-local input.
Syntax
Following is the syntax for returning the step value −
inputDatetimeLocalObject.step
Following is the syntax for setting the step value −
inputDatetimeLocalObject.step = number
Parameters
The number parameter can have the following values −
| Value | Description |
|---|---|
| seconds | Valid values are numbers that divide 60 evenly (e.g., 1, 2, 5, 10, 15, 20, 30) |
| milliseconds | Valid values start with "." and represent fractions of a second (e.g., 0.1, 0.01, 0.001) |
| "any" | No restrictions on the step value |
Return Value
The step property returns a string representing the step value of the datetime-local input field.
Example − Getting and Setting Step Value
Following example demonstrates how to get and set the step property of a datetime-local input −
<!DOCTYPE html>
<html>
<head>
<title>Input DatetimeLocal step Property</title>
</head>
<body style="font-family: Arial, sans-serif; padding: 20px;">
<h2>Datetime-Local Step Property</h2>
<label for="datetimeInput">Select Date and Time:</label>
<input type="datetime-local" id="datetimeInput" step="2">
<br><br>
<button onclick="getStep()">Get Current Step</button>
<button onclick="setStep('10')">Set Step to 10 seconds</button>
<button onclick="setStep('0.1')">Set Step to 0.1 seconds</button>
<p id="result"></p>
<script>
var input = document.getElementById("datetimeInput");
var result = document.getElementById("result");
function getStep() {
result.innerHTML = "Current step value: " + input.step;
}
function setStep(value) {
input.step = value;
if (value.indexOf('.') === -1) {
result.innerHTML = "Step set to " + value + " seconds";
} else {
result.innerHTML = "Step set to " + value + " seconds (milliseconds precision)";
}
}
</script>
</body>
</html>
The output shows how the step property can be accessed and modified −
Current step value: 2 Step set to 10 seconds Step set to 0.1 seconds (milliseconds precision)
Example − Complete Form with Step Validation
Following example shows a complete form demonstrating the step property with different values −
<!DOCTYPE html>
<html>
<head>
<title>DatetimeLocal Step Property Demo</title>
<style>
.form-container {
max-width: 600px;
margin: 0 auto;
padding: 20px;
font-family: Arial, sans-serif;
}
.input-group {
margin: 15px 0;
}
label {
display: block;
margin-bottom: 5px;
font-weight: bold;
}
input[type="datetime-local"] {
width: 100%;
padding: 8px;
border: 1px solid #ccc;
border-radius: 4px;
}
button {
background-color: #007bff;
color: white;
padding: 8px 15px;
border: none;
border-radius: 4px;
cursor: pointer;
margin: 5px;
}
button:hover {
background-color: #0056b3;
}
#output {
background-color: #f8f9fa;
padding: 10px;
border-radius: 4px;
margin-top: 15px;
}
</style>
</head>
<body>
<div class="form-container">
<h2>DateTime-Local Step Property</h2>
<div class="input-group">
<label for="myDatetime">Select Date and Time:</label>
<input type="datetime-local" id="myDatetime" step="1">
</div>
<div>
<button onclick="changeStep('1')">1 Second Step</button>
<button onclick="changeStep('5')">5 Second Step</button>
<button onclick="changeStep('0.001')">Millisecond Step</button>
<button onclick="changeStep('any')">Any Step</button>
<button onclick="showCurrentStep()">Show Current Step</button>
</div>
<div id="output">Current step: 1 second</div>
</div>
<script>
var datetimeInput = document.getElementById("myDatetime");
var output = document.getElementById("output");
function changeStep(stepValue) {
datetimeInput.step = stepValue;
if (stepValue === "any") {
output.innerHTML = "Step set to: any (no restrictions)";
} else if (stepValue.indexOf('.') !== -1) {
output.innerHTML = "Step set to: " + stepValue + " seconds (with millisecond precision)";
} else {
output.innerHTML = "Step set to: " + stepValue + " second(s)";
}
}
function showCurrentStep() {
var currentStep = datetimeInput.step;
output.innerHTML = "Current step value: " + currentStep;
}
</script>
</body>
</html>
This example provides buttons to set different step values and displays the current setting. The datetime-local input will only accept time values that conform to the specified step interval.
Step set to: 5 second(s) Step set to: 0.001 seconds (with millisecond precision) Step set to: any (no restrictions) Current step value: any
Browser Compatibility
The datetime-local input type and its step property are supported in modern browsers. However, some older browsers may not fully support the datetime-local input type. Always test your implementation across different browsers to ensure compatibility.
Key Points
-
The step property controls the precision of the seconds component in datetime-local inputs.
-
Step values can be whole numbers (seconds) or decimal numbers (fractional seconds).
-
Setting step to "any" removes all step restrictions.
-
The property returns a string value, not a number.
-
Invalid step values may cause the input to behave unexpectedly.
Conclusion
The HTML DOM Input DatetimeLocal step property provides precise control over the time intervals allowed in datetime-local input fields. By setting appropriate step values, you can ensure users select times that meet your application's requirements, whether you need second-level precision or millisecond accuracy.
