Datetime input for current or future time - html

The default value of the form input should be the current time. The user should can select to provide a time in the future and than a normal datetime field should be shown. Can this be realized without javascript and how?

You can leave the input empty and change it to the current time on the backend (or maybe set it to the current time at the moment of rendering template and interpret past times as "now"). But the most you can do without JavaScript is adding a comment that empty input means current time. If you want to make some kind of fancy checkbox for current time and maybe hide the datetime input, you should do it with JavaScript

You can try to use <input name="date_input" type="date" id="date_input"> in your template.
And do views do :
from datetime import datetime
# get a string from a POST like "10/2/2018"
date_get = request.POST.get("date_input")
# format of date/time strings; assuming dd/mm/yyyy
date_format = "%d/%m/%Y"
# create datetime objects from the strings
date = datetime.strptime(date_get, date_format)
# Check if we have a datetime
print(date)
datetime.datetime(2018, 2, 10, 0, 0)

Related

Coldfusion datepicker dd/mm/yyyy but MySQL stay with format yyyy/mm/dd

Datepicker is an option for user easy to pick the date to fill the form. In coldfusion, there is a fill form that need to use datepicker and after user selected the date and fill the form with format yyyy/mm/dd by default format which is MySql can read. If i change into dd/mm/yyyy and click save into MySql will get error because from what i know default format for MySql is yyyy/mm/dd.
<input type="text" name="Date_joined" size="auto" style="border:0px"required="yes">
This is the function to popup datepicker :
<a href="javascript:showCal('Calendar1')">
This is logo for datepicker :
<img align="right" src="calendar_menu.gif" alt="Select a date" border="0"></a>
Is there any solution for user pick a date and input text will display dateformat dd/mm/yyyy but still can save into MySql without error.
How to make MySQL accept a user input in format DD/MM/YYYY so the data will be recorded
If you want to save a string of numbers and dashes that represents a date in the format you want it displayed on a screen, then just make your database column a CHAR(10) and be done with it.
But, if you want to do calculations against it, aggregate data by it, DO THINGS with it, then save it as a date type. Don't worry about how your database UI represents that date value to you. Maybe it's different from how you want it shown on an HTML page, it doesn't matter. What matters is that as a date object, you can easily use and display that value however you like.
From what I know, MySQL will only accept datatype date with format YYYY/MM/DD.
Not how that works.
https://dev.mysql.com/doc/refman/5.7/en/datetime.html
The DATE type is used for values with a date part but no time part.
MySQL retrieves and displays DATE values in 'YYYY-MM-DD' format. > The supported range is '1000-01-01' to '9999-12-31'.
See that "retrieves and displays" (emphasis mine)? It's just a date object with "00:00:00" as the time portion.
So however your form field accepts the string representation of the date, you need to convert it to a proper date object. Per Dan's suggestion, you can easily use parseDateTime() to accomplish this.
#writeOutput( parseDateTime( now() ) )# will output {ts '2018-03-14 15:29:19'}.
If your form field contains a valid string that represents a date (e.g. 2018-03-14):
#writeOutput( parseDateTime( form.myDateField ) )# will output {ts '2018-03-14 00:00:00'}.
So the value will be saved as a date object, without the time portion. When you read the saved value later, just use dateFormat() to display it in any format you like.
(Moved from comments for greater visibility)
One important addition to Adrian's answer. In this specific case, you MUST use a date mask with parseDateTime(). The mask controls how parseDateTime() interprets the input. Without the correct mask, the results may be wrong for your specific input, namely dd/mm/yyyy.
TryCF Example
Code:
<cfscript>
dateString = "05/08/2018";
writeOutput("<br>Without mask = "& parseDateTime( dateString) );
writeOutput("<br>With mask = "& parseDateTime( dateString, "dd/MM/yyyy") );
</cfscript>
Result:
Without mask = {ts '2018-05-08 00:00:00'} (May 8, 2018)
With mask = {ts '2018-08-05 00:00:00'} (August 5, 2018)

How to use Time Data type only in Rails?

I use time as datatype in my rails migration but when I am retrieving it in model
it includes date. I wanted to be able to compute time difference without date.
Any way I can do to make my model return time only ?
I wanted this 03:15:00 not this 2000-01-01 05:54:42
If you want to convert a Datetime object into a Time object, you can use Time.parse.
date_and_time = Time.now # 2000-01-01 05:54:42
time_only = Time.parse(date_and_time) # 05:54:42
If you use the time data type for a column in your database migration you should be fine. Rails fills the missing fields (in this case year, month, day) with default values since the data type requires them. Since all responses get these default values you should be able to calculate time differences without date since all values have the same default value. If you want to get the time as string back in the format you want you can use #strftime method.
Eg:
time.strftime('%H:%M:%S')

How to separate time from datetimepicker vb.net

I have one datetimepicker which custom format MM/dd/yyyy h:mm tt
I have database and has a column "Date_Time" the value of the DateTimePicker is saved to the column Date_Time formatted like this MM/dd/yyyy h:mm tt
now i want to get the Time only not the entire value of datetimepicker just the hh:mm tt from the column Date_Time
SORRY FOR MY GRAMMAR
How about DateTime.TimeOfDay?
It returns the time that has elapsed since midnight (which is what h:mm tt stands for in your code).
Dim Time As TimeSpan = DateTimePicker1.Value.TimeOfDay 'Would return for example 3:14 PM
The answer above is right.
If you need to get time string, you can use also another way, which includes a formating:
Dim myTimeString = DateTimePicker1.value.ToString("hh:mm")
You can do that for any part of the DateTime value.
You are heading for a new problem. If you zero out the Date portion and store the result to a DateTime column, you will end up storing something like: 0001-01-01 16:43:12. A column defined as DateTime will always have a Date, as will a DateTime variable.
The first problem may be getting MySQL to accept a non-Date in a DateTime column. Using a column defined as DateTime(3), mine throws a generic fatal error exception trying to store just a TimeSpan to it:
cmd.Parameters.Add("#p3", MySqlDbType.DateTime).Value = DateTime.Now.TimeOfDay
cmd.ExecuteNonQuery()
If MySqlDbType.Time is used as the type, I get an exception that the time is an invalid value for the column...and it is.
If you manage to store it somehow, the next problem will be when/if you want to put that value back in a DateTimePicker: the minimum date you can enter is 1/1/1753 (first full year of the current calendar). So your DateTime var with the Date zeroed out wont work. You'll first have to restore the date portion, but the Date, Year etc are all readonly.
Solution 1
Define the column as Time(0) which will store hours, minutes and seconds. Use the value in the parens to specify fractional seconds, for instance Time(3) will also store milliseconds. When you read the data, store it to a TimeSpan.
Then in your UI use a different control, otherwise you have the same problem - adding some Date data to it to make it usable in a DateTimePicker
Solution 2
Use a DateTimePicker and a DateTime column, but just ignore the Date portion in your code. This will allow you to use what is in the Database as is with the control.
You can get the time selected with DateTime.TimeOfDay but storing and reusing it may be problematic.

HTML fIeld with datetime ticks

I'm calling a webservice through Ajax that only accepts datetime parameters as a 'long' type (representing the ticks).
Is there anyway to have a datetime input field on a form that represents this?
I would like to have a Date and Time selector (with a calendar and those kind of eye candy) that internally send the field value (or at least exposes the value) as Ticks.
I'm going to assume by "ticks" you mean milliseconds since epoch. You could use an existing plugin like http://eonasdan.github.io/bootstrap-datetimepicker/. It supports both date and time pickers. From there, simply grab the value and run it through new Date('supported format here').getTime() to get the milliseconds value. If you're looking for a pure HTML5 way, unfortunately that isn't possible.

rails datetime field in edit form says UTC

I have a datetime field and the value is stored in mysql like this:
2014-08-26 16:00:00
When user selects datetime, they select it from a jquery datetime popup. And its inserted into text field and sent to the server at which point it is parsed like this:
def date_start=(s)
datetime = parse_datetime s
write_attribute :date_start, datetime
end
def parse_datetime(s)
DateTime.strptime(s,'%m/%d/%Y %I:%M %p')
end
And then inserted into the database. Now when the user edits the form, the datetime is displayed in the text field, but it is displayed like this:
2014-08-26 16:00:00 UTC
Why does it say UTC there (the record is retrieved and stored local time) and how can I get rid of that?
It's considered good practice to store timestamps in your database as UTC time, regardless of which timezone they were originally (so they can be accurately compared and prevent messes that usually arise when things aren't consistent).
Active Record takes care of this by doing the conversion automatically. That means that when a user selects a datetime, you have to ensure it's given the right timezone in Rails/Ruby when it is inputted, using Time.use_zone() or something similar. And similarly when displaying it after retrieving from the database. You'll need some way to retrieve the user's local timezone (Javascript or a manually selected profile option).
As an aside, if you want to set the default timezone for your application to something other than UTC, then you can change the config.time_zone option in application.rb. Note that the database will still store things in UTC, it just allows you to set a sensible default timezone for datetime input and output (i.e. a default timezone for Active Record to convert to/from). It won't help with individual user datetimes though, unless all your users reside in the same timezone.
You shouldn't override writer at all. Rails shoudl handle it properly.
In form show datetime like this:
f.text_field :date_start, (f.object.date_start.strftime('%m/%d/%Y %I:%M %p') if f.object.date_start ) ...
or make a method
def form_date_start
date_start.strftime('%m/%d/%Y %I:%M %p') if date_start
end
and in form
f.text_field :date_start, f.object.form_date_start