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

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

Related

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.

Formatting date with HtmlDisplayFor and DateTime.ToString difference

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

Make dates in datetimepicker grey?

Im writing a hotel booking system with VS2012 which uses ms sql and in the booking i have two datetimepicker controls. When i select a date in the first one the date automatically gets transfered to the second datetimepicker. I now want when the user opens up the second datetimepicker not being able to select a date that is before the first date from the first datetimepicker. I dont want to use if method displaying a message i just want the dates to be "greyed" out.
Is that possible and how?
*edit (screenshot of how i would like it to be viewed or similar http://i43.tinypic.com/2zpimvs.jpg )
*edit2: its not the control(dateTimePicker2) it self i want to grey out, its the dates inside it that is smaller then the date selected in dateTimePicker1. The screenshot provided is in the area of what im looking for.
You could try this:
DateTimePicker2.MinValue = Convert.toDateTime(DateTimePicker1.SelectedDate);
EDIT
Standard controls don't support the grey-out of a datetimepicker. If you want to do this, you need to subclass the datetimepicker or look at a premade control like a DateEdit from devexpress (i think they even have free controls).
Change the second datepicker MinDate to first DatePicker selected date .. like below ..
rdpDatePicker2.MinDate = rdpDatePicker1.SelectedDate.Value;

Modify Date/Time from SQLDataSource

I have a date column in my database, it's not date time so the values have no time attached.
However, when I drag and drop a SQLDataSource on my page and bind a dropdownlist to it to display the date, it automatically adds a time of 12:00:00 AM.
I know why it does this, but I don't know how to get rid of it. Do I need to bind it from codebehind?
Is there an easy fix to this?
Set DropDownList.DataTextFormatString.
<asp:DropDownList
id="datesList"
runat="server"
dataTextFormatString="{0:dd/MM/yyyy}"/>
Then after DataBinding your control will apply the correct format:
Add format to field or column definition. For example Gridview:
<asp:boundfield datafield="Your_Date_Column" dataformatstring="{0:MMMM d, yyyy}" htmlencode="false" />
BTW, other method is to use ViewModel - class which is needed only for displaying data. In this class there is no need to use DateTime or other data types: only string. For example, you can define some displaying defaults here: if value null, show text "UNDEFINED" or what you need. In some cases this approach makes centralized point where you make preparation of data for display.
Can you show your sql query it may be useful .Have you use convert(varchar,date ,103) method to your query
C# doesn't have date datatype and though values from sql(date) datatype is convert to datetime field in c#. So better convert date as nvarchar in sql using convert(varchar,date ,103) though C# treats it as string.

HyperLinkField in Gridview not showing link on column's items

I have a question today about the usage of HyperLinkField in a GridView. I have searched nearly everything, but nothing I try is working. I am running and ORACLE database and all of the fields below are string type EXCEPT for RECORD_DATE and RECORD_TIME. They are of type 'DATE'.
This is what I have:
<asp:HyperLinkField HeaderText="MODEL_NUMBER" DataTextField="MODEL_NUMBER" SortExpression="MODEL_NUMBER" DataNavigateUrlFields="MODEL_NUMBER, SERIAL_NUMBER, DEFECT_CODE, RECORD_DATE, RECORD_TIME"
DataNavigateUrlFormatString="~/AllAudits.aspx?Model={0}&Serial={1}&Defect={2}&RecordDate={3}&RecordTime={4}" />
Visual Studio has no complaints with the string, and the page loads fine and everything, but it isn't showing a link for the column labeled "MODEL_NUMBER" like it should be.
I tried:
NavigateUrl="~/AllAudits.aspx"
And that created a link, but I don't think I have the ability to pass parameters through this.
Can anyone point me in the correct direction on this?
It may have something to do with how you are passing in the date types. This thread seems to resolve the problem you are having.

Categories

Resources