user1765862
user1765862

Reputation: 14155

Parsing Integer Value As Datetime

I have date represented as integer like 20140820 and I want to parsing it as datetime, like 2014.08.20.

Do I need to parse each integer value (2014)(08)(02) using index or is there simpler way?

Upvotes: 36

Views: 124546

Answers (5)

Jodrell
Jodrell

Reputation: 35716

So, we have two competing implementations,

using System;
using System.Globalization;
                    
public class Program
{
    public static void Main()
    {
        int i = 20140820;
        Console.WriteLine($"StringParse:{StringParse.Parse(i)}");
        Console.WriteLine($"MathParse:{MathParse.Parse(i)}");
    }
}

public static class StringParse
{
    public static DateTime Parse(int i)
    {
        return DateTime.ParseExact(
            i.ToString().AsSpan(),
            "yyyyMMdd".AsSpan(),
            CultureInfo.InvariantCulture);
    }
}

public static class MathParse
{
    public static DateTime Parse(int i)
    {
        return new DateTime(
                Math.DivRem(Math.DivRem(i, 100, out var day), 100, out var month),
                month,
                day);
    }
}

The string approach is probably safer and probably handles edge cases better. Both will be fast and unlikely to be a performance bottle neck but it is my untested assertion that the math approach probably has better performance.

Upvotes: 0

Hossein Sabziani
Hossein Sabziani

Reputation: 3495

int sampleDate = 20140820;
var dateFormat = DateTime.ParseExact(sampleDate.ToString(), "yyyyMMdd",
     CultureInfo.InvariantCulture).ToString("yyyy.MM.dd");

result:

2014.08.20

Upvotes: 0

Soner Gönül
Soner Gönül

Reputation: 98750

If your CurrentCulture supports yyyyMMdd format as a standard date and time format, you can just use DateTime.Parse method like;

int i = 20140820;
DateTime dt = DateTime.Parse(i.ToString());

If it doesn't support, you need to use DateTime.ParseExact or DateTime.TryParseExact methods to parse it as custom date and time format.

int i = 20140820;
DateTime dt;
if(DateTime.TryParseExact(i.ToString(), "yyyyMMdd", 
                          CultureInfo.InvariantCulture,
                          DateTimeStyles.None, out dt))
{
    Console.WriteLine(dt);
}

Then you can format your DateTime with .ToString() method like;

string formattedDateTime = dt.ToString("yyyy.MM.dd", CultureInfo.InvariantCulture);

Upvotes: 61

Kartikeya Khosla
Kartikeya Khosla

Reputation: 18873

Try This :-

string time = "20140820";
DateTime theTime= DateTime.ParseExact(time,
                                    "yyyyMMdd",
                                    CultureInfo.InvariantCulture,
                                    DateTimeStyles.None);

OR

string str = "20140820";
string[] format = {"yyyyMMdd"};
DateTime date;

DateTime.TryParseExact(str, 
                          format, 
                          System.Globalization.CultureInfo.InvariantCulture,
                          System.Globalization.DateTimeStyles.None, 
                          out date))

now date variable will have required converted date of string '20140820'

Upvotes: 1

Lanorkin
Lanorkin

Reputation: 7504

The easiest and most performance way would be something like:

int date = 20140820;

int d = date % 100;
int m = (date / 100) % 100;
int y = date / 10000;

var result = new DateTime(y, m, d);

Upvotes: 25

Related Questions