Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a datepicker which shows date in format dd/MM/yyyy(i know i coould change there itself but by client want it that way) and in database its in format MM/dd/yyyy so i do want to convert in that way.

e.g. in text box 23/09/2010 and in c sharp its convert to mm/dd/yyyy
if i convert throuth below code,

C#
public static String DMYToMDY(String input)
    {
        return Regex.Replace(input,
        @"\b(?<day>\d{1,2})/(?<month>\d{1,2})/(?<year>\d{2,4})\b",
        "${month}/${day}/${year}");
    }

i am getting error (String specified is not a valid dateformat) while converting date format.

string temp = DMYToMDY("26/10/2012");
DateTime Frm1 = DateTime.Parse(temp);

now i need 10/26/2012 with datetime format.
please help on this..!
Posted
Comments
Sarrrva 18-Oct-12 6:15am    
Its Simple try first or second Answer friend

regards
sarva
mahi37 18-Oct-12 7:18am    
Dear Sarrrva, i gave sol to chage the date into different string formats.
if i change dd/mm/yyyy to mm/dd/yyyy the above one is working for strings.
now i want dateTime format which is (mm/dd/yyyy)in string format? because i would like to call that for sp.

One way of doing it will be

C#
DateTime dt;

dt.CustomFormat = "mm/dd/yyyy HH:mm:ss";


You can use the above example and modify to your requirement.
 
Share this answer
 
v2
Hi try this one,


C#
public static String DMYToMDY(String input)
       {
           return Regex.Replace(input,
           @"\b(?<day>\d{1,2})/(?<month>\d{1,2})/(?<year>\d{2,4})\b",
           "${month}/${day}/${year}");
       }

       private void button1_Click(object sender, EventArgs e)
       {
           string temp = DMYToMDY("26/10/2012");
           DateTime Frm1 =DateTime.Parse(temp);
           MessageBox.Show(Convert.ToDateTime(Frm1).ToString("MM/dd/yyyy"));
       }


regards
sarva
 
Share this answer
 
Comments
mahi37 18-Oct-12 6:26am    
this is ok. but i want to chage that into datetime format which is in Message Box?
If i use Convert.ToDateTime again or datetime.parse(Convert.ToDateTime(Frm1).ToString("MM/dd/yyyy")) not working again i am getting error "String specified is not a valid dateformat"
Very simple, actually UK format which handles the dd/mm/yyyy format.

C#
DateTime res = DateTime.Parse("26/10/2012", CultureInfo.GetCultureInfo("en-gb"));


res will be 10/26/2012

other way to do it

C#
string finalDate = Convert.ToInt32("26/10/2012".Split('/')[1]) +"/" + Convert.ToInt32("26/10/2012".Split('/')[0]) + "/" + Convert.ToInt32("26/10/2012".Split('/')[2]);


Hope this helps :)
 
Share this answer
 
v2
You can use DateTime.TryParseExact to help you to do this.
First,
C#
using System.Globalization;

C#
DateTimeFormatInfo dateFormat = new DateTimeFormatInfo();
dateFormat.DateSeparator = "/";

string clientInputDate = "31/01/2012";

DateTime date1 = DateTime.MinValue;

if (DateTime.TryParseExact(clientInputDate , "dd/MM/yyyy", dateFormat, DateTimeStyles.AllowWhiteSpaces, out date1))
{
    // clientInputDate is a valid date
}
else
{
    // WinForm
    MessageBox.Show("clientInputDate is not a valid recognised date format.");
    // ASP.NET
    Response.Write("clientInputDate is not a valid recognised date format.");
}

string yourDesireDateFormat = date1.ToString("MM/dd/yyyy");
 
Share this answer
 
v4
Comments
mahi37 18-Oct-12 6:50am    
this is throwing an exception
adriancs 18-Oct-12 6:55am    
I can execute the code with no exception. What is the exception throwing?
adriancs 18-Oct-12 7:00am    
I modify the above code. I replace the throw exception if with MessageBox.Show() and Response.Write().
I replace this:
throw new Exception("clientInputDate is not a valid recognised date format.");
to this:
// WinForm
MessageBox.Show("clientInputDate is not a valid recognised date format.");
// ASP.NET
Response.Write("clientInputDate is not a valid recognised date format.");
mahi37 18-Oct-12 7:06am    
same error as "Response.Write("clientInputDate is not a valid recognised date format.");
u r giving again string format (yourDesireDateFormat).
actually i want it into the dateformat
adriancs 18-Oct-12 7:23am    
You want from this
dd/MM/yyyy
to
MM/dd/yyyy
isn't it?
C#
public string DMY_MDY(string d)
{
    string tm = "";
    if (d.Length > 10) 
    {
        tm = d.Substring(10);
        d = d.Substring(0, 10);
    }
    string[] a = d.Split("/");
    if (a.Length > 1)
    {
        return a[1] + "/" + a[0] + "/" + a[2] + tm;
    }
    return "";
}

23/09/2010 -> 09/23/2010
Happy Coding!
:)
 
Share this answer
 
v4
Comments
Sushil Mate 18-Oct-12 6:25am    
how about this one :)

string finalDate = Convert.ToInt32("26/10/2012".Split('/')[1]) +"/" + Convert.ToInt32("26/10/2012".Split('/')[0]) + "/" + Convert.ToInt32("26/10/2012".Split('/')[2]);
Aarti Meswania 18-Oct-12 6:32am    
nothing
various ways to solve a problem
correct Answer is must nothing else
you have do spliting 3 time and I have just 1 time

but do not worry i will not downgrade your answer you will loss more points than me If I downvote

and your answer is also giving desire output like mine
so, no matter to downvote

Good day!
:)
Sushil Mate 18-Oct-12 6:36am    
:) no need to shout on me.. because i didn't .. people has their own opinion.. some finds helpful.. some finds complex..

& i am not up to the mark to down vote you (you have more points than me :P )
Sushil Mate 18-Oct-12 6:44am    
& i was just showing you the simple way to do it.. didn't mean to offend you :)
Aarti Meswania 18-Oct-12 6:47am    
it's okay
single line doesn't mean it is fast in execution
thought once how many times you used inline functions

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