Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# format a string

Tags:

string

c#

format

I have a String that looks like

4/2/2012 12:00 AM
12/30/1899 10:00 AM

I want to format the strings so the first date/time stamp looks like

4/2/2012

The second should look like

10:00 AM

SHould I use the whitespace as a means to split the string?

like image 900
Cocoa Dev Avatar asked Jun 27 '26 00:06

Cocoa Dev


2 Answers

For complete string -->

string s= DateTime.Parse("4/2/2012 12:00 AM").ToString("d/M/yyyy hh:mm tt");

For separated strings -->

string date=DateTime.Parse("4/2/2012 12:00 AM").ToString("d/M/yyyy");
string time = DateTime.Parse("4/2/2012 12:00 AM").ToString("hh:mm tt");
like image 92
Marshal Avatar answered Jun 29 '26 15:06

Marshal


use the following function:

     string dstr = "4/2/2012 12:00";        
     DateTime dtime=Convert.ToDateTime(dstr )
     string dt1 = dtime.ToShortDateString();//to get the date
     string dt2 = dtime.ToShortTimeString();//to get the time
like image 36
st mnmn Avatar answered Jun 29 '26 15:06

st mnmn