how to show only date? - asp.net

I have a column in the data table called CurrentDate as datatype string (12/2/1983).
When I am displaying on the screen this is showing as 12/2/1983 12:00:00AM.
I am not sure why I am getting timestamp here?
Can anybody help me out?

If you are binding this from a datasource, like a SQL Data Source, you can use CONVERT in your query:
CONVERT(VARCHAR(10),nameOfColumn,101) AS Date
If you need to do this in C#, try the ToShortDateString() method:
string date = myDate.ToShortDateString();

In C# if you convert it to a DateTime object:
DateTime.ToShortDateString();

Are you using data binding to a gridview, formview, or other formatable data source? If so you can simply use the "d" format flag, like below:
<asp:BoundField DataField="MyDate" DataFormatString="{0:d}" HeaderText="My Date" />

An example of its usage within the context of setting a label would be:
MyLabel.Text = DateTime.Now.ToString("A FORMATTING STRING HERE);
Two very useful MSFT posts I use are: "Standard DateTime formatting strings" and "Custom date time formatting strings"

Related

Show date in UTC in ASP:GridView BoundField

I'm trying to use a GridView to show a DateTime value. So far I have been able to show the data using the format string that I want by using:
<asp:GridView ID="myGrid" ... >
<Columns>
<asp:BoundField DataField="myField" DataFormatString={0:MM/dd/yyyy hhmmss} ... />
...
</Columns>
...
</asp:GridView>
The problem I'm having now is that this is always being shown as local time. My users will be expecting it to always be shown as UTC, regardless of where they are. Is there some way I can force the GridView to show what the UTC time would be, instead of the local time?
For example, I have found out that in vb.net, to convert a string to a date I can tell it to use universal time with:
Dim myDateAsString as String = "03/31/2014 19:00"
Dim myDate as DateTime = DateTime.ParseExact(myDateAsString, "MM/dd/yyyy HH:mm", System.Globalization.DateTimeFormatInfo.InvariantInfo, System.Globalization.DateTimeStyles.AssumeUniversal)
I'm looking for something similar to the .AssumeUniversal part that I can use when I set up my GridView in asp.
I hope what I'm asking is clear. If not I can try to elaborate more, if someone let's me know what isn't clear.
UPDATE:
The DataSource for my GridView is a DataSet that is filled by a SELECT query that just pulls data from my database. Since the field I'm interested in here is a DateTime field in my database, I am assuming it will still be a DateTime when it gets assigned as the DataSource. Is that correct?
It looks to me like the only code related to filling the GridView is
myGrid.DatqaSource = mySQLObject.sel_from_table(vars)
myGrid.DataBind()
Where mySQLObject.sel_from_table(vars) will return a DataSet as described above. The answers so far have suggested that I need to change the data to UTC before sending it to the GridView - Where/how should that be done? Do I have to change it in my query somehow? Or in the DataSet before I assign it as the DataSource?
The grid view is merely a display mechanism. It'll display whatever value you pass it in the format you specified. If you want to change the value itself (Utc Date) then 2 options are:
Template Item where you can call an Eval option to compute Utc Date.
Change the value of myField server side so that the grid view can bind that. e.g. in your SQL query results.
Either ways, your server side code needs to convert the date into UTC (ToUniversalTime() or DateTime.UtcNow etc.) and send it across for display.
here is how you can do using TemplateField.
in your ASPX
<asp:TemplateField HeaderText="My Fields">
<ItemTemplate>
<asp:Label runat="server" text='<%# GetUtcFormattedDate((DateTime)Eval("myField")) %>' />
</ItemTemplate>
</asp:TemplateField>
In your code behind, you can have a static method
public static string GetUtcFormattedDate(DateTime date)
{
return date.ToUniversalTime().ToString(YOUR_DATE_FORMAT_STRING);
}
You could convert the time to UTC in your query to pull the data from the database.
In your select clause just use the below.
SELECT DATEADD (HH, 'Your UTC Offset', DateYouWantSelected)

asp.net convertion of date with mysql database

which is better convert the date by database or c# to bind it on grid event rowDataBound ? ,
and why & when we use this or this ?
If you are converting the date to some other date, it would be more efficient to do the conversion in C# and save the database some processing.
If you are trying to format the date, use the DataFormatString on the bound field.
You don't "convert" the date on the scenario you describe; you simply format it according to your needs. The date should be a date type on the database side and on the presentation layer, whatever it may be, can be displayed according to your needs. For example in asp.net you can format your date on the GridView as follows:
<asp:boundfield datafield="SomeDate" DataFormatString="{0:MM/dd/yyyy}" />
or
<asp:boundfield datafield="SomeDate" DataFormatString="{0:dd/MM/yy}" />

GridView Column filtering

I have a GridView control set up, populated from a database through the BL, and one of the fields contains currency amounts. Right now it is displaying in the format of 0000.0000, and I need to bring it to a currency format that adds $ + dataNumber down to hundredths.
I have looked for filters, but so far I haven't seen a direct and simple answer. Could using a regular expression to filter work? :/
It is my first attempt to do so. Any answers are appreciated, but please provide site documentation for me to reference while attempting to implement your suggestions.
The GridView.BoundColumn type provides the DataFormatString property you can use to specify the display format. You can use the C format to display in currency format.
<Columns>
...
<asp:BoundColumn DataField="MyFieldName" DataFormatString="C" />
...
</Columns>

<asp:ControlParamenter /> and nonstandard (en-US) DateTime binding

Database date format: "yyyy-MM-dd"
C# default date output: "MM/dd/yyyy"
My date format: "dd/MM/yyyy"
How do I use the asp:SqlDataSource with an asp:ControlParameter connected to an asp:TextBox using my date format? Specifically, I wanted to know
if there was a way to do this without having it done in code-behind
if I'm bound to code-behind, how to do it?
I've read about the Culture class, but I'm not sure that's the right path to explore. I've also had a check on some stackoverflow posts and came accross this Binding Date of DateTime in a control, but I've never seen this structure of Text formatting before in ASP.NET.
Thanks in advance,

How do I do 2 way databinding for DateTime fields using ObjectDataSource

I have an ASP.Net 3.5 page that contains a FormView control, bound to a Business Object using an ObjectDataSource.
One of the properties of the business object is a DateTime type, and I want to do 2 way databinding for this object, including the DateTime property.
I use a custom format for displaying the DateTime property, as shown here:
<asp:TextBox ID="TextBoxDate" runat="server" Text='<%# Bind("Date", "{0:d MMM yyyy HHmm}") %>' />
It displays just fine.
The problem is when I attempt to perform an Update. I get the following exception:
String was not recognized as a valid DateTime.
My ObjectDataSource contains an explicitly set UpdateParameter for this Property, but it doesn't appear to make any difference.
<UpdateParameters>
<asp:Parameter Name="Date" Type="DateTime" />
</UpdateParameters>
What am I doing wrong?
UPDATE:
It turns out that if I change my format string in my Bind expression to
{0:d MMM yyyy HH:mm}
(Notice the colon between the HH and the mm)
... then the two way data-binding works as expected. Sadly, this isn't exactly what I wanted. I was hoping to use the 24 hour clock without a colon, hence my original format string. This is still not working, and I would love to know why? And even better, I'd love to know how I can work-around this shortcoming in the framework, but still do declarative databinding.
Thanks.
You could override the ItemUpdating event of the FormView control and then modify the value for that parameter to make sure it's in the right format. Most likely this means:
get the value from e.NewValues("Date")
Parse the string value into a DateTime object
assign the value back to e.NewValues("Date")
I've had to do something like this in the past with currency fields where people might enter a dollar sign which would cause errors if left alone.
Bind() method, actually provide two way data binding and Eval provide one way data binding
In your scenario, you have to modify date in your formview databound event when you are showing data to user rather setting date format in bind property.
In this way you will get not error when update your fields.

Resources