How to obtain only the date, without the time, of a Datetime?

Asked

Viewed 16,833 times

3

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?

  • What is the type of parcelamento.data_vencimento? This is important to know how you need to get the date. There are even other things that would need to be analyzed to see if you can use the Convert.ToDateTime, i’m pretty sure this is a mistake. Doing what was suggested in the answer below doesn’t change anything: https://dotnetfiddle.net/TX6n3c

  • If you answer me this, I’ll improve the answer.

  • Thank you already worked out..

  • But it would be good to clarify so you can help other people. Take a look at [tour] to know more how the site works.

  • My data_maturity variable is in Datetime format, and by default gridview, when rendering, calls the . Tostring() of the field. I had to change this behavior using datastringformat in the gridview field itself

  • Did any of the answers solve the problem? Do you think you can accept one of them? See [tour] how to do this. You’d be helping the community by identifying the best solution. You can only accept one of them, but you can vote for anything on the entire site.

Show 1 more comment

2 answers

4

It all depends on the type of parcelamento.data_vencimento. He is a DateTime? So there’s nothing to be done, if the guy says there should be a date and a time, that’s what’s in it. What you can do when you need to submit is to pick up only the date, without the time, but this is a matter of presentation.

Do not confuse stored data with data to be presented. They are two different things. Presented data is always a string and may be in the format you think best. It doesn’t matter so much that the other answer doesn’t do anything useful, see.

The parcelamento.data_vencimento is string? I doubt it. If it is, you will find it difficult to manipulate it. If you only need to present it, then .ToString("dd/MM/yyyy") resolves.

using static System.Console;
using static System.Convert;

public class Program {
    public static void Main() => WriteLine(ToDateTime("03/25/2015").ToString("dd/MM/yyyy"));
}

Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.

Almost always storing only the date is a mistake and would need a good reason to do this. The reasons I see people "having" to use a guy just with a date are usually boring. If you really want to do it, I recommend using a library that does this well, such as Nodatime of Jon Skeet.

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

If you are brave, create a framework for this.

Depending on what you are going to use you would need to analyze how to build a new date, if you need to pass the date members alone, whether to pass a string, if it can be formatted, if it can pass a DateTime for him to convert.

Another thing you should look at is whether you can use the Convert.ToDateTime(). This is not a good option to catch strings external that you do not have full control.

3

I recommend you take a look at the documentation of struct DateTime at MSDN.

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

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.