Home > Back-end >  Format DateTime without converting it to a string in c#?
Format DateTime without converting it to a string in c#?

Time:01-07

In my app, I need to have a DateTime variable with this format: "dd.MM.yyyy HH:mm:ss" but it should not be a string.

These are the things that I've tried so far:

    var now = DateTime.Now.ToString("dd.MM.yyyy HH:mm:ss");
    var currentTime = DateTime.ParseExact(now, "dd.MM.yyyy HH:mm:ss", CultureInfo.InvariantCulture);
    var currentTime1 = DateTime.ParseExact(now, "dd.MM.yyyy HH:mm:ss", CultureInfo.CurrentCulture);
    var turkishCultureInfo = CultureInfo.CreateSpecificCulture("tr-TR");
    var currentTime2 = DateTime.ParseExact(now, "dd.MM.yyyy HH:mm:ss", turkishCultureInfo);
    var currentTime3 = DateTime.ParseExact(now, "dd.MM.yyyy HH:mm:ss", null);

    Debug.Log("now: "   now); // 06.01.2023 21:25:27
    Debug.Log("currentTime: "   currentTime); // 1/6/2023 9:25:27 PM
    Debug.Log("currentTime1: "   currentTime1); // 1/6/2023 9:25:27 PM
    Debug.Log("currentTime2: "   currentTime2); // 1/6/2023 9:25:27 PM
    Debug.Log("currentTime3: "   currentTime3); // 1/6/2023 9:25:27 PM

As you can see above, I tried ParseExact method with CultureInfo.InvariantCulture, CultureInfo.CurrentCulture, turkishCultureInfo and null all have the same output. 1/6/2023 9:25:27 PM, but I need like this 06.01.2023 21:25:27

I don't want to use string because I need to use methods like Subtract, AddSeconds that's why I need a DateTime type variable. Is there a way to get "DateTime.Now" formatted as "dd.MM.yyyy HH:mm:ss"?

CodePudding user response:

I think there is a bit of a confusion here what the string concatenation does to your values.

It is nothing less than simply using the default format for a parameterless ToString()!

Each of your strings like

Debug.Log("currentTime: "   currentTime);

basically internally implicitly equal

Debug.Log("currentTime: "   currentTime.ToString());

In that case the ToString() without parameters can be omitted since c# already uses it implicitly.

=> You are using string in all your examples! How else do you think anything is logged (as a string!) to the console? ;)

As noted in the comments a DataTime is just a point in time and basically just a kind of wrapper around a long which are the system ticks passed since the Unix Epoch.

All the other properties, values and formats are calculated based on those ticks on demand (which makes it quite expensive btw).

Is there a way to get "DateTime.Now" formatted as "dd.MM.yyyy HH:mm:ss"

Yes, exactly how you did it.

So what you want to do is just use DateTime for all your calculations and AddMinutes etc. and then only when you need to display the (current) value you simply pass in the correct format just like you did in your first line

const string format = "dd.MM.yyyy HH:mm:ss";

and

Debug.Log("currentTime: "   currentTime.ToString(format));

CodePudding user response:

you can create DateTimeExtensions class, to get string in expected forma easly:

public static class DateTimeExtensions
{
    const string myFormat = @"dd.MM.yyyy HH:mm:ss";
    public static string GetInMyFormat(this DateTime dt)
        => dt.ToString(myFormat);
}

and use it:

var myDateTime = DateTime.Now.AddHours(-36.23);
Debug.Log("myDateTime in my format: "   DateTime.Now.GetInMyFormat());
  • Related