Click here to Skip to main content
15,896,912 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how to get right format with datetime i want to save date in the format of dd/mm/yyyy
and my code is
C#
 protected void calFirstAnnounceDate_SelectionChanged(object sender, EventArgs e)
    {
        txtFirstAnnounceDate.Text = calFirstAnnounceDate.SelectedDate.ToString("d/M/yyyy", System.Globalization.CultureInfo.GetCultureInfo("hi-IN").DateTimeFormat);
        calFirstAnnounceDate.Visible = false;
    }

tenderDetail.FirstAnounceDate = DateTime.ParseExact(txtFirstAnnounceDate.Text, "d/M/yyyy",null);

but it an error comes String was not recognized as a valid DateTime.
Posted
Comments
José Amílcar Casimiro 19-Apr-13 6:48am    
try using "dd-MM-yyyy" instead of "d/M/yyyy".

C#
try
   {
       string dateformat = "dd/MM/yyyy";
       string[] ass = s.Split('/');
       if (ass[0].Length == 1)
       {
           ass[0] = "0" + ass[0];
       }
       if (ass[1].Length == 1)
       {
           ass[1] = "0" + ass[1];
       }
       s = ass[0] + "/" + ass[1] + "/" + ass[2];
       DateTime dt = DateTime.ParseExact(s, dateformat, null);
       // DateTime dt = Convert.ToDateTime(s);
       string day = dt.Day.ToString();
       string month = dt.Month.ToString();
       string year = dt.Year.ToString();
       s = year + "/" + month + "/" + day;
       s = s.Replace("/", "-");
   }
   catch (Exception err)
   {
   }
   return s;









try it
 
Share this answer
 
See this article written by OriginalGriff: Formatting a DateTime for display - format string description[^]
Some other information you'll find here: Indian Number & Date Format[^]

Sometimes, to correctly write date into SQL database you need to SET DATEFORMAT[^].
 
Share this answer
 
user DateTime.ToString("dd/MM/yyyy).
 
Share this answer
 

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