locked
How to format datetime to date only. RRS feed

  • Question

  • User-1447786422 posted

    I have a sql database with a smalldatetime datatype, i bind the data to a gridview, now i want to format the datetime in my c# to dd/mm/yyyy, can anyone help?

    Wednesday, July 25, 2018 8:42 AM

Answers

  • User-821857111 posted
    Text='<%# Eval("YourDate", "{0:dd/mm/yyyy}") %>' />
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 25, 2018 8:45 AM
  • User-158363518 posted

    hi

    change your data type to DATE (In SQL )

    CAST(yourField as DATE)

    -----

    DateTime dt = DateTime.ParseExact(yourObject.ToString(), "MM/dd/yyyy hh:mm:ss tt", CultureInfo.InvariantCulture);

    string s = dt.ToString("dd/M/yyyy", CultureInfo.InvariantCulture);

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 26, 2018 4:50 AM
  • User753101303 posted

    And NgaySinh is really a DateTime ? Are you sure it is not formatted earlier (on the db side ?) and already a string ?

    Edit: try https://msdn.microsoft.com/en-us/library/bz9tc508.aspx for how to force the culture. If without any format you still have the same pattern then it's likely for now you really try to show a string rather than a DateTime.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 26, 2018 7:27 AM

All replies

  • User-821857111 posted
    Text='<%# Eval("YourDate", "{0:dd/mm/yyyy}") %>' />
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 25, 2018 8:45 AM
  • User-1447786422 posted

    I have already use your code but nothing happen

    Wednesday, July 25, 2018 9:05 AM
  • User-821857111 posted

    What do you mean by "nothing happens"? Nothing appears? The value appears but isn't formatted? Something else? Perhaps you could show how you are using the code.

    Wednesday, July 25, 2018 12:05 PM
  • User753101303 posted

    Hi,

    Or I would configure my site to use a culture using this (French ?) and then I would use the "short date pattern" :

    Or Text='<%# Eval("YourDate", "{0:d}") %>' />

    The benefit is that if later want to support additional cultures it will be show the suitable format for whatever culture is in use.

    As pointed it is best to tell what actually happens. If you mean YourDate is still shown with dots it could be that / are not the literal value but date separators. If the value is just unchanged could it be that this is not currently a date but maybe already a string ???

    Wednesday, July 25, 2018 12:19 PM
  • User-1447786422 posted

    I put your code in to my textbox, but the value still not change 

    <asp:TextBox ID="txtNgaySinh" runat="server" Width="167px" Text='<%# Eval("NgaySinh", "{0:dd/mm/yyyy}") %>'></asp:TextBox>

    The textbox i use to get the value from datagrid and use it for update

    every time i choose a row in data grid the value apear in my text box with a patern yyyy-dd-mm hh:mm:ss now i want to format the date so when i choose a row it apear like dd/mm/yyyy

    Thursday, July 26, 2018 1:21 AM
  • User-1447786422 posted

    Can u show me how to do that? I mean using culture partern

    Thursday, July 26, 2018 1:22 AM
  • User1724605321 posted

    Hi Tuan1296,

    Please provide more details about your scenario , the textbox is inside  template of gridvew , or it is outside the gridview and when click gridview row , the value/date should display in textbox . If that is your requirement , you could directly format the datetime in textbox using c# :

    txtNgaySinh.Text = DateTime.Now.ToString("dd/MM/yyyy");

    Best Regards,

    Nan Yu

    Thursday, July 26, 2018 3:17 AM
  • User-1715638843 posted

    use 

    .Tostring("MM/dd/yyy)"

    or try

    <asp:BoundField DataField="MyDate" DataFormatString="{0:MM/dd/yyyy}" />

    Mark as answer if correct

    Thursday, July 26, 2018 4:44 AM
  • User-158363518 posted

    hi

    change your data type to DATE (In SQL )

    CAST(yourField as DATE)

    -----

    DateTime dt = DateTime.ParseExact(yourObject.ToString(), "MM/dd/yyyy hh:mm:ss tt", CultureInfo.InvariantCulture);

    string s = dt.ToString("dd/M/yyyy", CultureInfo.InvariantCulture);

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 26, 2018 4:50 AM
  • User753101303 posted

    And NgaySinh is really a DateTime ? Are you sure it is not formatted earlier (on the db side ?) and already a string ?

    Edit: try https://msdn.microsoft.com/en-us/library/bz9tc508.aspx for how to force the culture. If without any format you still have the same pattern then it's likely for now you really try to show a string rather than a DateTime.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 26, 2018 7:27 AM
  • User-1447786422 posted

    Oh god, u are right, i set it to string and forget about it, thank for the advide

    Thursday, July 26, 2018 8:08 AM