Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display only date and no time

In MVC razor, I am putting current date in the database like this..

model.Returndate = DateTime.Now.Date.ToShortDateString(); 

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..

EDIT:

In the controller I have

var model = new ViewModel(); model.ReturnDate = DateTime.Now;             return PartialView("PartialView", model);   

In the partialview, I have

@Html.EditorFor(model => model.Returndate) 

This is where its displaying the date as Date and Time together... I want just the date to be displayed. Not the time. I hope this edit explains better.

like image 734
ZVenue Avatar asked Aug 19 '11 16:08

ZVenue


People also ask

How do I only display date from DateTime?

ToString() − One more way to get the date from DateTime is using ToString() extension method. The advantage of using ToString() extension method is that we can specify the format of the date that we want to fetch. DateTime. Date − will also remove the time from the DateTime and provides us the Date only.

How to get Timestamp from date?

Getting the Current Time Stamp If you instead want to get the current time stamp, you can create a new Date object and use the getTime() method. const currentDate = new Date(); const timestamp = currentDate. getTime(); In JavaScript, a time stamp is the number of milliseconds that have passed since January 1, 1970.

What is the format of DateTime?

For example, the "d" standard format string indicates that a date and time value is to be displayed using a short date pattern. For the invariant culture, this pattern is "MM/dd/yyyy". For the fr-FR culture, it is "dd/MM/yyyy". For the ja-JP culture, it is "yyyy/MM/dd".


1 Answers

Just had to deal with this scenario myself - found a really easy way to do this, simply annotate your property in the model like this:

[DataType(DataType.Date)] public DateTime? SomeDateProperty { get; set; } 

It will hide the time button from the date picker too.

Sorry if this answer is a little late ;)

like image 176
Lazy Dave Avatar answered Nov 03 '22 00:11

Lazy Dave