Input type DateTime - Value format? - html

In which format should I put the date and time, for use in the HTML5 input element with datetime type?
I have tried:
1338575502
01/06/2012 19:31
01/06/2012 19:21:00
2012-06-01
2012-06-01 19:31
2012-06-01 19:31:00
None of them seem to work.

For <input type="datetime" value="" ...
A string representing a global date and time.
Value: A valid date-time
as defined in [RFC 3339], with these additional qualifications:
•the literal letters T and Z in the date/time syntax must always be uppercase
•the date-fullyear production is instead defined as four or
more digits representing a number greater than 0
Examples:
1990-12-31T23:59:60Z
1996-12-19T16:39:57-08:00
http://www.w3.org/TR/html-markup/input.datetime.html#input.datetime.attrs.value
Update:
This feature is obsolete. Although it may still work in some browsers,
its use is discouraged since it could be removed at any time. Try to
avoid using it.
The HTML was a control for entering a date and
time (hour, minute, second, and fraction of a second) as well as a
timezone. This feature has been removed from WHATWG HTML, and is no
longer supported in browsers.
Instead, browsers are implementing (and developers are encouraged to
use) the datetime-local input type.
Why is HTML5 input type datetime removed from browsers already supporting it?
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/datetime

For what it's worth, with iOS7 dropping support for datetime you need to use datetime-local which doesn't accept timezone portion (which makes sense).
Doesn't work (iOS anyway):
<input type="datetime-local" value="2000-01-01T00:00:00+05:00" />
Works:
<input type="datetime-local" value="2000-01-01T00:00:00" />
PHP for value (windows safe):
strftime('%Y-%m-%dT%H:%M:%S', strtotime($my_datetime_input))

This article seems to show the valid types that are acceptable
<time>2009-11-13</time>
<!-- without #datetime content must be a valid date, time, or precise datetime -->
<time datetime="2009-11-13">13<sup>th</sup> November</time>
<!-- when using #datetime the content can be anything relevant -->
<time datetime="20:00">starting at 8pm</time>
<!-- time example -->
<time datetime="2009-11-13T20:00+00:00">8pm on my birthday</time>
<!-- datetime with time-zone example -->
<time datetime="2009-11-13T20:00Z">8pm on my birthday</time>
<!-- datetime with time-zone “Z” -->
This one covers using it in the <input> field:
<input type="date" name="d" min="2011-08-01" max="2011-08-15"> This
example of the HTML5 input type "date" combine with the attributes min
and max shows how we can restrict the dates a user can input. The
attributes min and max are not dependent on each other and can be used
independently.
<input type="time" name="t" value="12:00"> The HTML5 input type
"time" allows users to choose a corresponding time that is displayed
in a 24hour format. If we did not include the default value of "12:00"
the time would set itself to the time of the users local machine.
<input type="week" name="w"> The HTML5 Input type week will display
the numerical version of the week denoted by a "W" along with the
corresponding year.
<input type="month" name="m"> The HTML5 input type month does
exactly what you might expect it to do. It displays the month. To be
precise it displays the numerical version of the month along with the
year.
<input type="datetime" name="dt"> The HTML5 input type Datetime
displays the UTC date and time code. User can change the the time
steps forward or backward in one minute increments. If you wish to
display the local date and time of the user you will need to use the
next example datetime-local
<input type="datetime-local" name="dtl" step="7200"> Because
datetime steps through one minute at a time, you may want to change
the default increment by using the attribute "step". In the following
example we will have it increment by two hours by setting the
attribute step to 7200 (60seconds X 60 minutes X 2).

This was a good waste of an hour of my time. For you eager beavers, the following format worked for me:
<input type="datetime-local" name="to" id="to" value="2014-12-08T15:43:00">
The spec was a little confusing to me, it said to use RFC 3339, but on my PHP server when I used the format DATE_RFC3339 it wasn't initializing my hmtl input :( PHP's constant for DATE_RFC3339 is "Y-m-d\TH:i:sP" at the time of writing, it makes sense that you should get rid of the timezone info (we're using datetime-LOCAL, folks). So the format that worked for me was:
"Y-m-d\TH:i:s"
I would've thought it more intuitive to be able to set the value of the datepicker as the datepicker displays the date, but I'm guessing the way it is displayed differs across browsers.

This works for setting the value of the INPUT:
strftime('%Y-%m-%dT%H:%M:%S', time())

That one shows up correctly as HTML5-Tag for those looking for this:
<input type="datetime" name="somedatafield" value="2011-12-21T11:33:23Z" />

Related

HTML convert time value to proper formatting when pre-populating input field

I am trying to pre-populate a time field on an input form, and I'm getting the field from a database. I was successful in finding how to do this with the date field by putting this in my value attribute:
"value="{{shipment.pickup_date|date:'Y-m-d'}}">"
Is there a similar conversion I can make with a time value? I've tried "HH:mm:ss", "H:m:s", to no avail. I can see in via the Chrome Elements tab that the value is getting passed properly, it's just not getting displayed in the field.
You can provide a value to the input tag in your template by passing the shipment's pickup_date value to the time filter with the right format. Providing the right format is key here, since the additional a.m. in your current value is not accepted. As a result, type="time" expects something like H:M; where H is the hour in double digits, and M is the minutes in double digits, as well. This means that you should change your code to the following:
<div class="">
<input type="time" name="pickup_time" value="{{ shipment.pickup_date|time:'H:i' }}">
</div>
Here, H:i is the format that converts your time value to something like H:M.

ASP.NET Core 3 MVC : input type date not refreshing from asp-for

I have input fields which are dates (no time) and I use an input field with type=date for date selection. The value is tied to the model using the asp-for attribute. When a user incorrectly selects a FROM date that is later than the TO date, my action reverses the dates so they are displayed the correct way round. However, the HTML values maintain the previously selected values and not the newly posted, corrected values. In effect, the asp-for is ignoring the latest value in the model.
The Model property is a DateTime and not a string and I don't use DataAnnotations in this case.
<input type="date" asp-for="DateFrom"
min="#Model.EarliestDate.ToHtmlInputDate()"
max="#Model.LatestDate.ToHtmlInputDate()"
onchange="$('form').submit();" class="form-control">
I found a simple fix for the issue. I'm not sure if treating the value as a string rather than a DateTime will also be a cure but that rather gets away from the fact that fundamentally it is a date.
I already have a class extension helper to get the required HTML format date so I set this string result to the VALUE of the element and this appears to work in all scenarios
<input type="date" value="#Model.DateFromString" asp-for="DateFrom" min="#Model.EarliestDate.ToHtmlInputDate()" max="#Model.LatestDate.ToHtmlInputDate()" onchange="$('form').submit();" class="form-control">

Can't set seconds value of <input type="datetime-local" /> to be 0

So I have this class which has an attribute Start, of type DateTime.
public class ClosedPlatform {
public DateTime Start;
}
What I'm trying to do is bind the Start attribute of a ClosedPlatform instance to an input of type datetime-local. At the same time I've found that datetime-local inputs don't display DateTime seconds by default, so I'm trying to force it to display the seconds value of Start.
ClosedPlatform closedPlatform;
...
<input type="datetime-local" step="1" #bind="#closedPlatform.Start" #bind:format="yyyy-MM-ddTHH:mm:ss"/>
I've managed to set up the input to get it almost working. Setting step="1" seems to be the common advice online to get a datetime-local input to display seconds, and it does. However, my issue is that the input won't let me set the seconds value to be 0. If the seconds value of closedPlatform.Start is already 0 before the input element is first loaded, then 0 seconds is correctly displayed in the input. However, if the seconds value is then changed to anything but 0, I can't use the input controls to set it back to 0 again (neither typing it in nor using the up and down controls will work). If Start has a seconds value of 59 and I increase it by 1, the seconds value shows 0 for a split second before going back to 59 (the same if I try going from 1 down to 0). Whereas something like say, the minutes value, I can change between a zero and nonzero value just fine.
So I'm wondering is there a way to get a datetime-local input to let me input 0 for the seconds value? I would have thought more people would have had this issue but I couldn't find any discussion of it anywhere. I did find out that datetime-local seems like a relatively new input type and is seemingly only fully supported by Chrome, Edge and Opera (I'm using Chrome), so is this just a matter of a newer feature being a bit rough around the edges?
Introduction:
First of all, quoting Mozilla <input type="datetime-local"> docs:
Seconds are not supported.
It means, seconds are not supported by control, the control will never set values for seconds part. Using step="1" has only visual effect.
About your code, to work with dates, you should to remove seconds part from bind format, just "yyyy-MM-ddTHH:mm":
<input type="datetime-local"
#bind="#closedPlatform.Start"
#bind:format="yyyy-MM-ddTHH:mm" step="1"/>
Workaround:
If you want to work with seconds, you need to use a string instead a DateTime (and use typecasting if you want):
<h5>String: #datestr</h5>
<h5>DateTime: #(parseString(datestr))</h5>
<input type="datetime-local" #bind="datestr" step="1" />
#code {
string datestr = "2013-10-24T20:36:00";
protected DateTime? parseString(string s)
{
... your code to cast string to datetime
}
}
Disclaimer Don't try to use this on production, seconds are not supported. Some Internet navigators or devices will don't work with this workaround.
Try it at blazorfiddle.

HTML Input Time, where min is pm (late) and max is am (early)

I have an input box, with a type of "time". I want to have a late time (23:00pm) as a min value, and an early time (6:00am) as a max value - creating a range of 23pm - 6am. (ie. 11pm, 12pm, 1am, 2am, 3am, 4am, 5am, 6am).
I've tried using Javascript, although I want to use it as a last resort. I don't want the native component to show up with values that I don't want the user to select (for example, on mobile devices).
Setting the 'min' value to 00:00:00 and the 'max' value to "06:00:00" works as intended. It's when the min value is before midnight it becomes an issue.
I'd expect the min and max values to create a range, but that doesn't work as expected.
As stated in MDN docs:
Unlike many data types, time values have a periodic domain, meaning
that the values reach the highest possible value, then wrap back
around to the beginning again. For example, specifying a min of 14:00
and a max of 2:00 means that the permitted time values start at 2:00
PM, run through midnight to the next day, ending at 2:00 AM.
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/time#max
That said, it seems like this is not working properly in Chrome 76.0,
after a simple test with a min value greater than the max, all times fail the validation and the form just don't work.
I suggest https://timepicker.co/ since it will work cross browser.
input:invalid+.validity:after {
content: '✖';
}
input:valid+.validity:after {
content: '✓';
}
<form>
<label for="time1">3:00 to 6:00: </label>
<input type="time" min="3:00" max="6:00" name="time1" required>
<span class="validity"></span>
<hr>
<label for="time2">23:00 to 6:00: </label>
<input type="time" min="23:00" max="6:00" name="time2" required>
<span class="validity"></span>
</form>
I experimented a little using the :invalid CSS pseudo-class but I couldn't come up with anything.
Note that in Firefox (unlike Chrome), the browser does not include up and down arrows on any <input type="time"> form element, so the min and max attributes have no effect.
I note that Mozilla Developer Network says the following:
By default, <input type="time"> does not apply any validation to
entered values, other than the user agent's interface generally not
allowing you to enter anything other than a time value. This is
helpful (assuming the time input is fully supported by the user
agent), but you can't entirely rely on the value to be a proper time
string, since it might be an empty string (""), which is allowed. It's
also possible for the value to look roughly like a valid time but not
be correct, such as 25:05.
Source: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/time#Validation

Formatting the time input field with html

I have a simple operating booking system using php. I give the user an option to select a data and time from date and time input fields. Now for the booking system I only want the user to be able to select in hour blocks but the current format of the time picker is hours:minutes:seconds in the format 00:00:00. Is there any way to disable the minutes and seconds? My current code is as follows, just the default for the time picker.
<input type="time" name="time" id="time" />
Any help would be great if possible, I know its hard to format the date and time pickers and varies alot on web browser so i'm using google chrome latest version 48.0.2564.116. Thanks!
The HTML5 specification states that valid type=time input elements must have a valid time string which must have:
Two ASCII digits, representing hour, in the range 0 ≤ hour ≤ 23
A ":" (U+003A) character
Two ASCII digits, representing minute, in the range 0 ≤ minute ≤ 59
The seconds themselves are already optional (that's part 4, but I didn't quote that above). However this does mean that the value of these elements wouldn't be valid without the minute entry.
Perhaps you'd be better off with a type=number input element instead, with a min of 0 and a max of 11 or 23 depending on your preference:
<input type=number min=0 max=23 value=0 />
Use JSON.stringify() to convet the time to string.
Then use the following function
var time = str.substring(0, 3);
This will display time in hours only
Add step attribute.
<input type="time" name="time" id="time" step="3600"/>
This will help you formatting the timepicker of chrome:
http://www.html5tutorial.info/html5-date.php
So you can see, that the steps, you could choose, doesn't contain n "Hour" step.
Best solution would be an html timepicker and/or a Javascriptfunction