Click here to Skip to main content
15,888,461 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have date as int, which I'm taking it from the filename and time from the file field as a string.

I'm trying to get the expected output as per the string format. I'm getting error that string was not recognized as a valid datetime format

Any help is appreciated, Thank you in advance.

What I have tried:

C#
DateTime DTTM; 
int crdt = 20180418; 
string time = "15:20:37.6789" ;
 DTTM = DateTime.ParseExact(crdt + " " + time, "yyyy-MM-dd HH:mm:ss.FFFF tt", CultureInfo.InvariantCulture);


Expected output : 2018-04-18 15:20:37.6789 PM
Posted
Updated 5-Jun-18 5:33am
v3
Comments
Member 13812021 5-Jun-18 11:29am    
Is there a reason you are separating them? Where are the "crdt" and "time" coming from?

Clearly your string doesn't match your format.  Don't you want to use this format instead?

yyyyMMdd HH:mm:ss.FFFF

/ravi
 
Share this answer
 
DateTime.ParseExact() does not have a method that takes accepts an INT as an argument
System.DateTime.ParseExact[^]

Some methods don't like an INT within the method even though it gets concatenized into a string when executed.

Best bet would be to stringify ahead of time, perhaps this would work:
C#
DateTime DTTM; 
int crdt = 20180418; 
string time = "15:20:37.6789" ;

// new
string timestring= crdt.ToString() + time;

// altered
DTTM = DateTime.ParseExact(timestring, "yyyyMMdd HH:mm:ss.FFFF", CultureInfo.InvariantCulture);
 
Share this answer
 
Comments
Richard Deeming 6-Jun-18 11:24am    
In C#, int + string == string.

So crdt + " " + time will result in the string "20180418 15:20:37.6789", with no int in sight. :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900