How to get only the date, without the time, of a DateTime?

2

Converting a field using ToDateTime it brings the date and time.

parcelamento.data_vencimento = Convert.ToDateTime(reader["data_vencimento"]);

Is there any direct conversion to bring only the date without the time?

    
asked by anonymous 03.01.2016 / 00:11

2 answers

4

It all depends on the type of parcelamento.data_vencimento . Is he a DateTime ? So there's nothing to be done, if the guy says he should have a date and an hour, that's what he'll have in him. What you can do when you need to submit is to just pick the date, without the schedule, but this is a matter of presentation.

Do not confuse stored data with data to be presented. They are two different things. Given data is always a string and may be in the format that you think is best. It does not matter that the other answer does nothing useful, see .

What% of% is parcelamento.data_vencimento ? I doubt that. If it is, it will be difficult to manipulate it. If you need only to submit, then string resolves.

See working on dotNetFiddle .

Almost always storing just the date is a mistake and you would have to have a good reason to do so. The reasons I see people "having" to use a date type are often punctured. If you really want to do that, I suggest you use a library that does this well, such as Jon Skeet's NodaTime .

It seems to have some structure just with the date somewhere in .Net or some library attached to it, but for very specific usage, probably database, not for general use, so it is not easy to find.

If you're brave, create a framework for it.

Depending on what you want to use, you would need to analyze how to construct a new date, whether to pass the date members separately, whether to pass a string, whether it can be formatted, whether to pass% for him to convert.

Another thing to look at is whether you can use .ToString("dd/MM/yyyy") . This is not a good option to get external strings that you do not have full control.

    
03.01.2016 / 07:24
2

I recommend that you take a look at the documentation for struct DateTime on MSDN.

But what you want can be obtained through the Date property of an instance of DateTime . This property returns a DateTime that has only the part of the date with the zeroed hours (00:00:00 or 12:00:00 AM).

parcelamento.data_vencimento = Convert.ToDateTime(reader["data_vencimento"]).Date;
    
03.01.2016 / 01:01