DotVVM - DateTime json conversion not working - datetime

I'm new to DotVVM and just playing with it for the moment.
My model has a DateTime property
When i edit this in a form with
<dot:TextBox Text="{value: Datum}" size="20" />
Full datetime is shown like "2018-02-08T13:02:42.0000000"
Editing the string is possible, and sending the object back to the viewmodel works fine.
When i add a Formatstring like "yyyy-MM-dd" the property is NULL in the json sent to the app.
How can i fix this?
Is it possible to include my own JsonConverter for DateTime conversion? (In this example i only need the date property)

In order to use dates in TextBox, you need to add ValueType="DateTime" (and to use specific format, you can add FormatString="d" for example).

Related

How can i Convert the DatePicker Input to string

I am using the following Codes to get the date from the date picker
objDD_ml.EffDate=(DirectCast(drv.Cells(3).FindControl("rdpEffDate"), RadDatePicker).DateInput).ToString
But it does not return the date... It returns like 'DateInput'
How can i use the code to get the value for Effdate as a correct format of date
Anyone please help me..
Whenever you want date into string and in specific format you can always apply following :
objDD_ml.EffDate = (
DirectCast(drv.Cells(3).FindControl("rdpEffDate"), RadDatePicker).DateInput
).ToString("dd/MM/yyyy")
Try to cast the FindControl object into the corresponding ASP control externally. Then access the value of that control and convert it to .ToString
I don't know what control you are using, but it should be something like below -
objDD_ml.EffDate = ((RadDatePicker)(drv.Cells[i].FindControl("rdpEffDate")).SelectedDate.Tostring;

DateTime syntax reference

I understand there are no complete reference docs available for Razor as yet, but would like to know what the various date format options are. Such as:
DateTime.Now.Day
DateTime.Now.Month
DateTime.Now.Year
DateTime.Now.Date
DateTime.Now.TimeOfDay
DateTime.Now.AddDays(1)
etc etc
Is there a complete reference anywhere for DateTime?
How do I get just DD/MM/YYYY without 00:00:00 being appended for example?
The DateTime class is part of the .NET framework. See MSDN documentation.
To format dates, you can use the .ToString(string format) method:
#DateTime.Now.ToString("dd/MM/yyyy")
DateTime is actually a .NET type. The full documentation is available here: http://msdn.microsoft.com/en-us/library/system.datetime.aspx
For your particular question you would want to use the ToShortDateString() method:
DateTime.Now.ToShortDateString()

Error converting date for Calendar in asp.net

I have written code for two checkboxes:
string sdate= Convert.ToDateTime(txtFromDate.Value);
string edate=Convert.ToDateTime(txtEndDate.Value);
I am getting the following error: "String was not recognized as a valid DateTime".
Well, it's reasonably clear: the input string wasn't in an appropriate format. I suggest that instead of using Convert.ToDateTime, you use DateTime.TryParseExact and specify the format string (or strings) that you expect the user to enter.
Using TryParseExact instead of just ParseExact means you can detect if the user has entered an incorrect date without an exception being thrown - and you should check for that. Basically the return value of TryParseExact indicates success or failure, and an out parameter is used to capture the parsed date/time on success.
What I hadn't noticed to start with is that you're then trying to assign a DateTime value to a string. That's not going to work - but we can't advise you on what you should be doing instead without knowing what you want to do with the data. I suspect you want to change sdate and edate to be DateTime variables instead.

How do I apply date formatting and add attributes to a textbox in MVC3?

In my application, I want all dates to display in the MM/DD/YYYY format and I also want the text box to be limited to 10 characters and have a certain class applied to it.
To that end, I made a custom editor template for Date that looks like this:
#ModelType System.Nullable(Of DateTime)
#Html.TextBoxFor(Function(d) d, New With {.class="polkDate", .maxlength="10"})
Then in my view I just can just call #Html.EditorFor(Function(m) m.someDate) and this works pretty well. However, because I'm using TextBoxFor, it doesn't respect the DisplayFormat attribute that's applied to my model class (i.e. it's spitting out the time as well instead of formatting the date).
I would love to use EditorFor so that it would respect the formatting that I want, but you can't add attributes like class and maxlength. I also tried just using the plain old TextBox helper, but I don't know how to make it generate the correct ID so that model binding still works.
Anybody know a way to make this happen? I don't feel like what I want to do here is too outlandish.
This appears to do the trick:
#ModelType Date
#If Date.MinValue.Equals(Model) Then
#Html.TextBox("", Nothing, New With {.class="polkDate", .maxlength="10"})
Else
#Html.TextBox("", Model.ToShortDateString(), New With {.class="polkDate", .maxlength="10"})
End If
Looks like the framework automatically assigns the ID and name fields of the html input tag so everything works properly.

Struts2 time (HH:mm) mapping to action field

I have a simple struts2 from:
<s:form ...>
<s:textfield name="initialTime" maxlength="5" size="5" />
</s:form>
Where the user has to enter a time in HH:mm format. In the corresponding Action, I have a property:
private Date initialTime;
along with its getter/setter.
I am getting a validation problem, as the received initialTime (i.e. 10:20) is not understood as a valid Date.
What is the good approach to retrieve times ? Should I use a private String initialTime field instead of a Date ?
Thanks.
I think you'll have to change the property to a String and programatically transform it to whatever final format you want.
While I may see cases where the conversion from HH:mm to a Date may make sense, I think we can agree this is not a general scenario.
./alex

Resources