Click here to Skip to main content
15,891,607 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,
how to convert string date to datetime in c#

for example :

string s;
s= "june/2014";
this one how to convert datetime
datetime frmmonth;

my o/p come like this

frmmonth= june/2014

any one no please answer me thank you.
Posted
Comments
Ranjeet Patel 19-Jun-14 4:48am    
Hi you do not have date in your string.. so It might give some issue. otherwise you can use convert.ToDateTime("04/05/2014");
Sharathkumar k.r 19-Jun-14 4:51am    
No ranjeet patel, i want like this only.
if u know please answer me.
Ranjeet Patel 19-Jun-14 5:30am    
See SharathKumaar you don't have a date. you only have month and year. so if you want in dd/mm/yyyy the system doesn't know which date you want..


but if you want in some other format like yyyy/mm or yyyy/mmm then I can help..


A DateTime value doesn't know about a format - it's just a date and a time. You can create a new DateTime value with the relevant information:

DateTime expiry = new DateTime(Convert.ToInt32(ddExpYear),
Convert.ToInt32(ddExpMonth),
1);
... but how that is "saved" is entirely up to you. If you give us more information, we may be able to help you more. You can format it to a string easily enough:

string formatted = expiry.ToString("yyyy/MM");
... but that may not be what you're after.

string s;
s ="June/2014";
Console.WriteLine("frmmonth="Convert.ToDateTime(s).ToString("MMMM/yyyy"));
. This would help you out.

You can also get the date by the following way.
string s;
s ="10/June/2014";
Console.WriteLine("frmmonth="Convert.ToDateTime(s).ToString("dd/MMMM/yyyy"));



[Agent_Spock]
- Added Code Brackets
 
Share this answer
 
v3
check DateTime.ParseExact Method[^]
C#
DateTime frmmonth =DateTime.ParseExact(s,"MMMM/yyyy", CultureInfo.InvariantCulture);
 
Share this answer
 
v3
you don't have date in your string please refer the link below this might help you.
this link can help you

or you can take any date default if you want to convert in dd/mm/yyyy, for that you need to make your own function to add the hardcoded fixed date in your string then try to convert in date format
 
Share this answer
 
v2

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