hi
if i have this in a string : 10/13/2010 8:38:40 AM
how to make it to this: 13/10/2010 08:38:40
thank's in advance
hi
if i have this in a string : 10/13/2010 8:38:40 AM
how to make it to this: 13/10/2010 08:38:40
thank's in advance
Use DateTime.Parse()
to convert to a true DateTime object and then use the DateTime.ToString()
method to output to the format you desire (code example coming):
var dateTime = DateTime.Parse("10/13/2010 8:38:40 AM");
var formattedString = dateTime.ToString("dd/MM/yyyy HH:mm:ss);
Quick and dirty:
DateTime.Parse("10/13/2010 8:38:40 AM", new CultureInfo("en-US")).ToString(new CultureInfo("en-GB"));
Since I know that those formats are for those cultures. However, you can read more about datetime formatting at:
http://msdn.microsoft.com/en-us/library/zdtaw1bw.aspx
Standard formatting: http://msdn.microsoft.com/en-us/library/az4se3k1.aspx
Custom formatting: http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx
DateTime.ParseExact("10/13/2010 8:38:40 AM","MM/dd/yyyy h:mm:ss tt",CultureInfo.InvariantCulture).ToString("dd/MM/yyyy HH:mm:ss")
edited to make sure 24 hour clock is used in output
var strfrom = "10/13/2010 8:38:40 AM";
DateTime dt = DateTime.Parse(strfrom, new CultureInfo("en-US"));
Console.WriteLine(dt.ToString(new CultureInfo("en-GB")));
var curDate = DateTime.Now.ToString() ;
string customDateFormat = Convert.ToDateTime(curDate).ToString("dd/MM/yyyy");
another variant, one one line:
DateTime.Parse("10/13/2010 8:38:40 AM", new CultureInfo("en-US")).ToString("dd/MM/yyyy HH:mm:ss");
Or for a more general solution, just pass a format string to DateTime.ToString('formatString'). For example, what you want is DateTime.ToString("dd/MM/yyyy HH:mm:ss"). This allows you to make any format you want.
Example:
DateTime exDT = DateTime.Now;
string exOut = exDT.toString("dd/MM/yyyy HH:mm:ss");
Here's a cheat sheet!
You can use ":" where you want it
d....Short Date
D....Long Date
t....Short Time
T....Long Time
f....Full date and time
F....Full date and time (long)
g....Default date and time
G....Default date and time (long)
M....Day / Month
r....RFC1123 date
s....Sortable date/time
u....Universal time, local timezone
Y....Month / Year
dd....Day
ddd....Short Day Name
dddd....Full Day Name
hh....2 digit hour
HH....2 digit hour (24 hour)
mm....2 digit minute
MM....Month
MMM....Short Month name
MMMM....Month name
ss....seconds
tt....AM/PM
yy....2 digit year
yyyy....4 digit year