DateTime syntax reference - asp.net

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()

Related

DotVVM - DateTime json conversion not working

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).

Which culture is used by ObjectDataSource to convert string to DateTime

I've just described my problem here: ObjectDataSource fails to parse string to DateTime . After a few google queries I came across this link: ObjectDataSource – Cannot convert value of parameter value from System.String to System.DateTime , and short explanation:
One bug I’ve come across a few times and again just recently is when using an ObjectDataSource with a GridView to update dates. When doing the update the ObjectDataSource always uses the en-US culture and not the culture defined for the application.
The bug has been acknowledged by Microsoft but never fixed.
The workaround that involves manual parsing is ... unacceptable :) Do you know any other, more programmer friendly solution to that issue?
Although this issue was apparently never fixed, there are several workarounds that you may use provided in this link. Not sure why parsing would be unacceptable. It can be done in 1 line of code:
DateTime myDate = DateTime.Parse(myTextBox.Text, CultureInfo.CurrentCulture.DateTimeFormat);
Observe that the culture is not hardcoded (which is the only reason I could think of that would make this unacceptable).

invalid date syntax with ajax calendar

I had Ajax calendar and when I selected from it it give me date as (07/12/2010)
and when I made space between the year and / it converted to (2010/07/12) and this invalid date so pleas what the error.
That's not a error is it? It sounds to me like the Ajax is just setting the date as a culture invariant. See this article on CodeProject
Try Specifying the date Format by using calendar Format property to "dd/MM/yyyy"
http://www.ajaxlines.com/ajax/stuff/article/aspnet_ajax_calendarextender_and_validation.php
Check out http://www.ama3.com/anytime/
If you are working from JS you would be better off working from its numeric (epoch) representation and then convert it to whatever format you want to use or use the js date api to achieve what you want.
https://developer.mozilla.org/en/JavaScript/Reference/global_objects/date

Parse string pattern into Date in Flex

Is there some way in flex to parse strings to date. I want it to support custom formats similar to 'dateformatter'. Using 'dateformatter' class we can parse date object in various string formats as specified by 'formatString property'. I want it other way round, from string to date. The parse method 'Date.parse(my_string)' does string parsing but for a very limited set of formats. Can't we have something similar to following, where user can specify his/her own formats.
someformatter.formatString = 'HH::MM::SS' ;
mydate = someformatter.formatTodate('23::56:34');
Will 'parseDateString' method of dateformatter be useul here?
Thanks in advance.
You can use
DateField.StringToDate()
This is static function of DateField class, and returns Date object.

The string was not recognized as a valid datetime

objfile.dateFileDate=convert.ToDatetime(Format(txtdate.text,"MM/dd/yyyy hh:mm"))
following error is coming
The string was not recognized as a
valid datetime .There is an unknown
word starting at 0.
What should i do to save this datetime,
please help
You can't format normal text using datetime formats.
Try
C#
objfile.dateFileDate=DateTime.ParseExact(txtdate.text, "MM/dd/yyyy hh:mm", null);
VB.NET
objfile.dateFileDate=DateTime.ParseExact(txtdate.text, "MM/dd/yyyy hh:mm", Nothing)
This is assuming dateFileDate is a DateTime type and that the txtdate.text is in the above format.
If your program is used by a international crowd, read on :)
ppl from different cultures will write dates in diffrent formats, so if your always going to parse the string that could be get sticky. Consider using the calander control?
Im saying this based on personal experience.
Also finding out why your current one is failing, i would do a DateTime.Now.ToString() and compare that to whats in the textbox so you can see whats curently being typed in wrong ( While debugging off course, to help track down the problem)
Try hh:nn instead of hh:mm
I believe mm is Months in two digit format and nn is minutes in two digit format.

Resources