简体   繁体   中英

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.

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));
string str = "41305"; 
DateTime d = DateTime.FromOADate(Convert.ToDouble(str));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM