简体   繁体   中英

How to remove time from datetime in C#

In the Controller Page:

maLeads.Cdate = DateTime.Now;

In the Model Page:

public Nullable<System.DateTime> Cdate { get; set; }

when i use the .ToString("dd/mm/yyyy") and .ToShortTimeString format gives error. Can someone help me with this problem, please?

Probably because you're calling .ToString() from a null instance; you should go with

CDate.HasValue ? CDate.Value.ToString("dd/MM/yyyy"): ""

This will not remove the time part from your DateTime? object, but maybe you don't need that. If so, use the .Date property.

Also, notice you're using the wrong date format: " mm " means "minutes", not months (" MM ")

Try this

var dateAndTime = DateTime.Now;    
var date = dateAndTime.Date;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM