I'm working on my first project in asp.net core razor pages and trying to add a creation date time field to my Article model which is by default filled with current date time. I have defined it liked this:
[Required]
[DataType(DataType.DateTime)]
public DateTime CreationDate { get; set; } = DateTime.Now;
when I visit the Article creation page, it is shown like this:
2019/02/02 09:25:36.377 PM
I don't want the second and millisecond to be shown here, but couldn't find a way to remove them. Any help would be appreciated.
You problably want to either use DateTime.ToString(string) to show the date. For example CreationDate.ToString("yyyy/MM/dd HH:mm")
would become 2019/02/02 09:25
Or you can use the DisplayFormat Attribute . Example
[Required]
[DataType(DataType.DateTime)]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:yyyy/MM/dd HH:mm}")]
public DateTime CreationDate { get; set; } = DateTime.Now;
Why not just (put this in the getter)
var newdate = CreationDate.AddTicks(-CreationDate .Ticks % TimeSpan.TicksPerMinute);
Or try this out c# 6+
[Required]
[DataType(DataType.DateTime)]
public DateTime CreationDate { get; set; } = DateTime.Now.AddTicks(-DateTime.Now.Ticks % TimeSpan.TicksPerMinute);
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.