Click here to Skip to main content
15,899,679 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
my code is

C#
string s = Convert.ToDateTime(fl.CreationTime).ToString();

             flist.UploadDate = DateTime.ParseExact(s, "yyyy-MM-dd HH:mm tt", null).ToString();



i want date in 2014/12/10 4:39 PM this format
Posted
Updated 9-Sep-14 21:14pm
v4
Comments
Thanks7872 10-Sep-14 2:50am    
How fl.CreationTime looks like and what is the explected output?
RAHUL(10217975) 10-Sep-14 3:04am    
Can you tell what is the value of fl.CreationTime ?
george4986 10-Sep-14 3:45am    
'flist.UploadDate' declared as string/datetime?
try this
string s = Convert.ToDateTime(fl.CreationTime).ToString("yyyy-MM-dd h:mm tt").Replace('-', '/');

1 solution

There are a number of things here:
fl.CreationTime looks like a FileSystemInfo.CreationTime or a FileInfo.CreationTime - which is a DateTime already, so you don;t need to convert it:
So...
C#
string s = Convert.ToDateTime(fl.CreationTime).ToString();
Can be just:
C#
string s = fl.CreationTime.ToString();


Secondly, the default ToString for a DateTime is to use the current system settings - which is very, very unlikely to be in ISO format. So the string you generate will be along the lines of
15/02/2001 17:45
which will not match the format string you are telling ParseExact to expect. So ParseExact will fail, and throw an exception. Yo could provide a parameter to ToString to generate the correct format: Formatting a DateTime for display - format string description[^]...

But...why are you parsing the damn string anyway?
Why not just use the original DateTime that you started with?
C#
flist.UploadDate = fl.CreationTime;
should be all you need...
 
Share this answer
 
Comments
sanjaysgh 10-Sep-14 3:10am    
i want date in 2014/12/10 4:39 PM this format
Thanks7872 10-Sep-14 3:29am    
Have you gone through the link he suggested?
OriginalGriff 10-Sep-14 3:45am    
DateTime values do not have a format.
There are a number of milliseconds since an arbitrary point in time.
The only time they have a format is when they are converted to a string for presentation.

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



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