-2

I have a datetime in the format MM/dd/yyyy:

string datetime = "05/16/2018"

Now, as per the requirement, I need to convert this string to DateTime. Whenever I do so It removes leading zero.

DateTime dt = DateTime.Parse(datetime, Globalization.CultureInfo.InvariantCulture, Globalization.DateTimeStyles.None)

Here, It gives output as 5/16/2018 but I need it as 05/16/2018. So, how do I achieve this result?

SᴇM
  • 7,024
  • 3
  • 24
  • 41
Jeeten Parmar
  • 5,568
  • 15
  • 62
  • 111
  • 1
    You are posting this question again, I'm repeating: `DateTime` object does not have format. Formatting occurs when you want to show your `DateTime` object, so you are converting it to string and can choose in which format to show. Refer to [Custom Date and Time Format Strings](https://learn.microsoft.com/en-us/dotnet/standard/base-types/custom-date-and-time-format-strings) – SᴇM Dec 13 '18 at 06:39
  • @SeM I don't want to format it but can I get the same date `05/16/2018` while parsing in DateTime? – Jeeten Parmar Dec 13 '18 at 06:40
  • The DateTime value is the 16. May 2018. Period. When I would debug it I will see 16.05.2018 as value, because of the german formatting, but the value inside the DateTime is the 16. May 2018 – Sir Rufo Dec 13 '18 at 06:45
  • @JeetenParmar your question is unclear to me, `DateTime` is just an objects which stores number of ticks since 12:00 midnight, January 1, 0001 A.D. (C.E.) to your particular date, in the [GregorianCalendar](https://learn.microsoft.com/en-us/dotnet/api/system.globalization.gregoriancalendar?view=netframework-4.7.2) calendar. – SᴇM Dec 13 '18 at 06:48
  • Did any of the answers help you? If not, please tell me what you are still confused about. – Sweeper Dec 14 '18 at 07:06

3 Answers3

5

You need to understand that a DateTime is just an object that represents a point in time. It does not store information about how it is formatted. The following are the same DateTime, just in different formats:

05/16/2018
5/16/2018

because they represent the same point in time.

It's like how int behaves. It will "remove your leading zeros" as well:

int a = 05;
Console.WriteLine(a); // just prints "5".

Because 05 and 5 are the same int, as far as int is concerned.

Whether leading zeroes are added in a DateTime depends on how you format it. They only appear when you convert your DateTime to a string. 05/16/2018 and 5/16/2018 are different strings.

If you want the leading zeroes, just specify a date format like dd/MM/yyyy whenever you want to output it:

Console.WriteLine(dateTime.toString("dd/MM/yyyy")); // prints leading zeroes!
Sweeper
  • 213,210
  • 22
  • 193
  • 313
  • Your perception is somewhat wrong! I have checked that with a Test Console Application that your converted DateTime can contain leading zero without converting it to string if your machine DateTime format contain leading zero. – TanvirArjel Dec 13 '18 at 07:12
  • @TanvirArjel Whenever you are printing something, `Console.WriteLine` will call `ToString` on that object, which is "converting to string". – Sweeper Dec 13 '18 at 07:23
  • This was not the point! Point was that whether it contain leading zero if a string is converted into c# datetime. Yes! it can contain if machine datetime format contains leading zero because by default c# datetime format is machine datetime format. so there is nothing related to the behavior of `int`. – TanvirArjel Dec 13 '18 at 07:26
  • @TanvirArjel - No! `DateTime` **has no format** full stop! What you refer to as "machine datetime format" is a _string representation_ of the `DateTime` for whatever the machine culture is. The `DateTime`, however, has no format. A `DateTime` has no concept of _leading zeroes_ because it _has no format_! – Chris Dunaway Dec 13 '18 at 17:08
1

First of all your string DateTime format(MM/dd/yyyy) is confusing! It will give misleading value for a string DataTime value like "05/08/2018". So you have to use ParseExact instead of Parse as follows:

DateTime dt = DateTime.ParseExact(datetime,"MM/dd/yyyy", CultureInfo.InvariantCulture);

It gives output as 5/16/2018 but I need it as 05/16/2018. So, how do I achieve this result?

You can simply achieve this my changing you machine DateTime format to a leading zero DataTime format! bacause by default C# DateTime takes the format of the machine Datatime format.

Well, I have also checked with a Test Console Application to confirm that your converted DateTime can contain leading zero without converting it to string if your machine DateTime format contains leading zero.

TanvirArjel
  • 30,049
  • 14
  • 78
  • 114
0

"5/16/2018" is string presentation od DateTime value. If you want to output it with leading zero, you can use custom format:

dt.ToString("dd/MM/yyyy");
Backs
  • 24,430
  • 5
  • 58
  • 85
  • There is no issue with String format. But I need it as `DateTime` only so I need to parse it. – Jeeten Parmar Dec 13 '18 at 06:40
  • @JeetenParmar so, I don't understand your question. What does it mean "t removes leading zero."? Who removes? When? – Backs Dec 13 '18 at 06:42
  • `DateTime.Parse` removes leading zero and It returns result `5/16/2018` instead of `05/16/2018` – Jeeten Parmar Dec 13 '18 at 06:48
  • 1
    @JeetenParmar DateTime internal saves data just as `long` value. it does not have leadeng zeros - it's only string presentation of `long`. So you can output it with `0` or without, or anyway you want. But the value is the same everytime – Backs Dec 13 '18 at 06:54