Formatting date with HtmlDisplayFor and DateTime.ToString difference - c#

I have issue with date formatting mimatch between what is generated by Razor in the view with #Html.DisplayFor and the date I want to show from ajax call.
For some reason the date which is rendered by #Html.DisplayFor shows the format as "2015/04/20 14:14". Then I need to update one row in the view and I make some Ajax call which returns the updated date, I call .ToString on the Date object and it returns the "3/10/2017 2:31:10 PM"
I checked the Thread cultures and they are same when the View is generated and same when it reaches controller, but for some reason #Html.DisplayFor shows different date format.
I don't want to have two different places with date formatting, I want to have one place of generation, I know I can just call ToString in Display method, but I would like to know WHY they differ?
Anybody know this? Thanks

Related

How to display DateTime as Date when Convert.ToDateTime() was run first

i'm stuck regarding a value I want to show. The problem is that I want to display in a GridView a Date value, but I can't parse it so it does that. I've tried to shrink the table column and several ways of parsing. Thank you so much for the help As it is now. And this is how it's parsed at the moment reserva.Fecha = Convert.ToDateTime(dr["fecha"].ToString());
DateTime object has both date and time associated with it. If you want to display only the date part of it, use the ToString() to only show the date with formatting defined. See Documentation here
DateTime.Parse(dr["fecha"].ToString()).ToString("MM-dd-yyyy");
You can also use the method ToShortDateString() to display only the Date part of the DateTime object.
DateTime.Parse(dr["fecha"].ToString()).ToShortDateString()
It turns out that the table I created previously was unable (I think) of displaying the data I had (in a List object) I still can´t display it from the list I want, but it shows the data I tell it to, so for now it works. Finally, instead of using <asp:TemplateField> <HeaderTemplate> <asp:Label for my table, I used <asp:BoundField>. I'm sorry if i'm not clear enough on the explanation, but I do believe it was a problem caused by using code I didn't understand so I don´t think it would happen to anyone else. Thank you so much everyone https://imgur.com/a/IWKHHvO

CalendarView in UWP C# showing wrong day

I have CalendarView on two pages.
First page is used to add records to calendar by assigning the selected date to the database record.
Second page is used to show all records assigned to the selected date.
Now, when on the first page I assign date to the product by clicking the date manually, on the second page product is shown correctly when selected date that corresponds to the products date.
However, when on the first page I add dates to the products programmatically, on the second page, these products are shown one day before than they should.
I have no idea what is happening. I will post code if you want to check something.
THe problem is strange, becaue on one calendar these automatically added dates are shown for example on 5th April, but on the second calendar they are listed under 4th April.
Is that the globalization of two calendars problem?
Locale(globalization ) would be a potential problem. But having code would help us to provide you more info.
I have managed to resolve the problem. It was in deed issue with DateTimeOffset/DateTime values.
I changed every instance of Date I found in the code by appending it with
.LocalDateTime
.
This way I got the correct local date of each DateTimeOffset object and data is added to/ pulled from the Calendar correctly.

How to modify data before diplaying it in DevExpress Reports

I have several pages that contains a Report in each one, and I used a base class for these pages called ReportBasePage.
The Report shall view some date fields in it beside other fields, and now I want to do some modifications on the report parameters before displaying them on the report. How could I do that?
In other words, I want to change the date format from the base page before being displayed on the report.
If you want to show the date in different format, you can change the FormatString of the control that is displaying the date value (for example XRLabel). Set the FormatString to your desired date format (for example "{0:dd-MM-yy}")
If you need to change the value (not the format) of the report parameters, you can do it using the XtraReport event "ParametersRequestSubmit".
Regards.

JQuery Datepicker changing format from front end

I'm currently using JQuery to display a datepicker front end. I've added a dateFormat so that it displays the date selected in a more commonly used Gregorian, day-month-year (DMY)format (code snippet below).
$("#Filters_DateRangeFrom").datepicker({dateFormat: "dd-mm-yy"});
However when I debug I notice that the values passed in are being reversed. So say I enter 12/04/2014 - 15-04-2014, it will be shown as passing in 04-12-2014 -04-15-2014. The latter being 'null' as there is no 15th month.
It does the exact same if I take the dateFormat out. So on the front end its american and it gets reversed to english. Which works perfectly fine and brings up the correct data for the specified date ranges.
Any ideas on how to prevent the reversing of the formats?

Two CalendarExtenders?

I have a popup ajax calendarextender that populates a text box with the selected date, (MM/dd/yyyy). This works. I want to capture the same date in an additional text box with a different format (yyyyMMdd). The first is for display and the second date is a parameter for gridview query. Is this done with two calandarextenders? (btw I'm using C#)
If you are supplying one to a query, you could just change the parameter value in code before supplying it to the query.
var datetimeForQuery = extenderVariable.SelectedDate.ToString("yyyyMMdd");

Categories

Resources