Display date only

Eyualine

New member
Joined
Jun 3, 2024
Messages
1
Programming Experience
Beginner
Since the database field is a datetime datatype and I am converting the current date to string format, this is not working.. how can I do this? I am doing the string format because I want the date in mm/dd/yyyy format and not in mm/dd/yyyy hh:mm:ss time format..

i want show date like dd/mm/yyyy
but i dont why timestamp is showing like
dd/mm/yyyy 3:00 PM

<td>Updated dt.</td>

<td>
(item.linedeptdt != null)
</td>
 
If you just use a DateTime field/property and allow the system to convert it to a string, it will call the ToString overload with no parameters, so the system default format will be used. That will vary from system to system but it will almost always include the time. If you want a specific format then you need to explicitly call a ToString overload that accepts that format specifier. You can use standard format specifiers or custom format specifiers. For instance, myDateTime.ToString("d") will use the system short date format. That may vary from system to system but will not include the time regardless. In contrast, myDateTime.ToString("dd/MM/yyyy") will use that specific format on every system.
 
For ASP.NET Core you can set a property with the desired format as shown below using DisplayFormat.

code behind:
[BindProperty, DisplayFormat(DataFormatString = "{0:MM/dd/yyyy hh:mm:ss tt}",
     ApplyFormatInEditMode = true)]
public DateTime DateTime { get; set; }

HTML

HTML:
<table>
    <tr>
        <td style="text-align: left; width: 5em;font-weight: bold">Updated:</td>
        <td>@Model.DateTime</td>
    </tr>
</table>
 
Back
Top Bottom