mhar
mhar

Reputation: 47

c# String to Integer to DateTime ISO Format

I am a bit lost and need your help, this seems to be easy in PHP but I am new to C#.

I have a string

string str = "41305"; // equivalent to 01/31/2013, parse from excel reader.

I need to insert date in DBF file and I am getting mismatch data type. How can I format it to DateTime ISO Format 20130131? Thanks in Advance.

Upvotes: 0

Views: 535

Answers (2)

Habib
Habib

Reputation: 223247

Use DateTime.FromOADate method.

DateTime dt = DateTime.FromOADate(41305);

For output:

Console.WriteLine(dt.ToString("MM/dd/yyyyy"));

Output would be:

01/31/02013

Since you have the value in string, you can parse it to double using Double.Parse or Double.TryParse according to your requirement.

EDIT: Like:

string str = "41305";
DateTime dt = DateTime.FromOADate(double.Parse(str));

Upvotes: 6

Atish Kumar Dipongkor
Atish Kumar Dipongkor

Reputation: 10422

string str = "41305"; 
DateTime d = DateTime.FromOADate(Convert.ToDouble(str));

Upvotes: 1

Related Questions