How do I convert a datetime field to a string with the format 1st Feb 2011
in C# ? doj
is datetime
field in sql server.
string DateOfJoin = dt.Rows[0]["DOJ"].ToString();//2011-02-01 00:00:00.000
How do I convert a datetime field to a string with the format 1st Feb 2011
in C# ? doj
is datetime
field in sql server.
string DateOfJoin = dt.Rows[0]["DOJ"].ToString();//2011-02-01 00:00:00.000
First of all, 2/1/2011
can be 1st Feb or 2nd Jan not 1st Jan.
Second, let's parse your string
to DateTime
.
DateTime dt = DateTime.ParseExact(dt.Rows[0]["DOJ"].ToString(),
"M/d/yyyy hh:mm:ss tt",
CultureInfo.InvariantCulture);
or you can explicitly cast it to DateTime
DateTime dt = (DateTime)dt.Rows[0]["DOJ"];
Third, .NET does not have a built-in way in BCL to generate day suffix. But Lazlow write a method for that which works seems okey to me as;
static string GetDaySuffix(int day)
{
switch (day)
{
case 1:
case 21:
case 31:
return "st";
case 2:
case 22:
return "nd";
case 3:
case 23:
return "rd";
default:
return "th";
}
}
and you can this method like;
string DateOfJoin = String.Format("{0}{1} {2}",
dt.Day,
GetDaySuffix(dt.Day),
dt.ToString("MMM yyyy", CultureInfo.InvariantCulture));
which generates
use format below to get the similar without suffix see explanation
string DateOfJoin = dt.Rows[0]["DOJ"].ToString("dd MMMM yyyy");
However to get the suffix you'll need to break it up so that you get the day separate i.e.
string DateOfJoin = dt.Rows[0]["DOJ"].ToString("dd MMMM yyyy");
I would use below if you really need the suffix
string day = dt.Rows[0]["DOJ"].ToString("dd");
day = GetDaySuffix(Int32.Parse(day));
This using a function to add the suffix that I originally found here
string GetDaySuffix(int day)
{
switch (day)
{
case 1:
case 21:
case 31:
return "st";
case 2:
case 22:
return "nd";
case 3:
case 23:
return "rd";
default:
return "th";
}
}
string DateOfJoin = String.Format("{0} {1}", day, dt.Rows[0]["DOJ"].ToString("MMMM yyyy"));
Not tested but should be useful start
Try this
string DateOfJoin =Convert.ToDateTime( dt.Rows[0]["DOJ"]).ToString("dd MMMM yyyy HH:mm:ss");
string DateOfJoin =Convert.ToDateTime( dt.Rows[0]["DOJ"]).ToString("dd MMMM yyyy");
Depending on the specified culture, long date strings can be achieved by casting the object to DateTime and then calling ToString()
with the format parameter.
string DateOfJoin = ((DateTime)dt.Rows[0]["DOJ"]).ToString("D");
Further reference here
Edit: Formatting with things like '1st', '2nd', requires you to write a custom method which evaluates the day number and appends the correct string to it, something like:
private string GetSuffix(DateTime dt)
{
if(dt.Days % 10 == 1)
{
return dt.Days.ToString() + "st";
}
else if(dt.Days % 10 == 2)
{
return dt.Days.ToString() + "nd";
}
else if(dt.Days % 10 == 3)
{
return dt.Days.ToString() + "rd";
}
else
{
return dt.Days.ToString() + "th";
}
}
And then add this part of the string, to a ToString("Y") of the DateTime, like:
string DateOfJoin = GetSuffix((DateTime)dt.Rows[0]["DOJ"]) + " " + ((DateTime)dt.Rows[0]["DOJ"]).ToString("Y");
Var dt = DateTime.Parse(DateOfJoin);
dt.ToString('F');
See bottom of the following page for more formats
This tack can be accomplished with using the following approach:
DateTime date = new DateTime(2015, 01, 01);
// 1st Jan 2015
string s = Ordinal.Add(date.Day) + date.ToString(" MMM yyyy", System.Globalization.CultureInfo.InvariantCulture);
Here the Ordinal
class is implemented as follows:
static class Ordinal {
public static string Add(int num) {
if(num <= 0) return num.ToString();
switch(num % 100) {
case 11:
case 12:
case 13:
return num + "th";
}
switch(num % 10) {
case 1: return num + "st";
case 2: return num + "nd";
case 3: return num + "rd";
default: return num + "th";
}
}
}