I want to get this specific dateformat in c# Friday Nov 29th 2013
how can i achieve that ?
DateTime.Now.toString("//what should i write here ");
I want to get this specific dateformat in c# Friday Nov 29th 2013
how can i achieve that ?
DateTime.Now.toString("//what should i write here ");
You can use dddd MMM dd'th' yyyy
format like;
string s = "Friday Nov 29th 2013";
var date = DateTime.ParseExact(s, "dddd MMM dd'th' yyyy", CultureInfo.InvariantCulture);
Console.WriteLine(date);
Output will be;
11/29/2013 12:00:00 AM
Here a demonstration
.
For more informations, take a look at;
EDIT: Okey, looks like I little bit misunderstand your question. There is no standart format for "st"
, "nd"
, "th"
in .NET.
You can use
DateTime.Now.ToString("dddd MMM dd'th' yyyy");
but this works only for this case. When your day is 1
, you get 1th
instead of 1st
.
Here is the pattern:
If the units digit is: 0 1 2 3 4 5 6 7 8 9
write this number: th st nd rd th th th th th th
But also this pattern is not usefull because 11th
but 21st
. Looks like your only chance writing your own specific cases.
You can create your own custom format provider to get this specific dateformat "Friday Nov 29th 2013"
public class SuffiexFormattedDateProvider : IFormatProvider, ICustomFormatter
{
/// <summary>
/// Returns an object that provides formatting services for the specified type.
/// </summary>
/// <param name="formatType">An object that specifies the type of format object to return.</param>
/// <returns>
/// An instance of the object specified by <paramref name="formatType" />, if the <see cref="T:System.IFormatProvider" /> implementation can supply that type of object; otherwise, null.
/// </returns>
public object GetFormat(Type formatType)
{
if (formatType == typeof(ICustomFormatter))
return this;
return null;
}
/// <summary>
/// Converts the value of a specified object to an equivalent string representation using specified format and culture-specific formatting information.
/// </summary>
/// <param name="format">A format string containing formatting specifications.</param>
/// <param name="arg">An object to format.</param>
/// <param name="formatProvider">An object that supplies format information about the current instance.</param>
/// <returns>
/// The string representation of the value of <paramref name="arg" />, formatted as specified by <paramref name="format" /> and <paramref name="formatProvider" />.
/// </returns>
/// <exception cref="System.NotSupportedException"></exception>
public string Format(string format, object arg, IFormatProvider formatProvider)
{
if (!(arg is DateTime)) throw new NotSupportedException();
var dt = (DateTime)arg;
string suffix;
if (dt.Day % 10 == 1)
{
suffix = "st";
}
else if (dt.Day % 10 == 2)
{
suffix = "nd";
}
else if (dt.Day % 10 == 3)
{
suffix = "rd";
}
else
{
suffix = "th";
}
return string.Format("{0:dddd MMM} {1}{2}, {0:yyyy}", arg, dt.Day, suffix);
}
}
Then you can use this as
string formatDateString = string.Format(new SuffiexFormattedDateProvider(), "{0}", DateTime.Now);
Output:- "Friday Nov 29th, 2013"
D? F? It depends what kind of format you want! http://msdn.microsoft.com/en-us/library/az4se3k1(v=vs.110).aspx
Try
DateTime now = DateTime.Now;
Console.WriteLine(now.ToString("D"));
DateTime.Now.toString("//what should i write here ");
If we are limited to just DateTime.ToString(string)
, I am not aware of any way to do this. There is no format specifier that performs the "st", "nd", "rd", "th" computation for English ordinal numbers
I'd see if you can get the requirements changed so that you don't have to worry about "st", "nd", "rd" and "th". If you must format in this way, this post might help.
Take a look at this article, should have what you want
http://www.csharp-examples.net/string-format-datetime/
from that article:
DateTime dt = new DateTime(2008, 3, 9, 16, 5, 7, 123);
String.Format("{0:y yy yyy yyyy}", dt); // "8 08 008 2008" year
String.Format("{0:M MM MMM MMMM}", dt); // "3 03 Mar March" month
String.Format("{0:d dd ddd dddd}", dt); // "9 09 Sun Sunday" day
String.Format("{0:h hh H HH}", dt); // "4 04 16 16" hour 12/24
String.Format("{0:m mm}", dt); // "5 05" minute
String.Format("{0:s ss}", dt); // "7 07" second
String.Format("{0:f ff fff ffff}", dt); // "1 12 123 1230" sec.fraction
String.Format("{0:F FF FFF FFFF}", dt); // "1 12 123 123" without zeroes
String.Format("{0:t tt}", dt); // "P PM" A.M. or P.M.
String.Format("{0:z zz zzz}", dt); // "-6 -06 -06:00" time zone
If you are lazy like me you could just do this.....
// Lets create a date to work with.
DateTime DT1 = DateTime.Now;
// Create a holding string, and add the day name, with a comma and then the day.
string FormattedDatestringThing = DT.ToString("dddd") + ", " + DT1.Day.ToString());
// Now lets add the "st", "nd", "rd" or "th" depending on the day value.
switch (DT1.Day)
{
case 1:
case 21:
case 31:
FormattedDatestringThing += "st"; // So if it's 1, 21 or 31 then add "st".
break;
case 2:
case 22:
FormattedDatestringThing += "nd"; // If it's 2 or 22 then add "nd".
break;
case 3:
case 23:
FormattedDatestringThing += "rd"; // If it's 3 or 23 then add "rd".
break;
default:
FormattedDatestringThing += "th"; // Otherwise write out "th".
break;
}
// Finaly add the year to the string and we are done.
FormattedDatestringThing += " " + DT1.ToString("yyyy"));
Easy enough to stick into a function if needed. Note: This is the lazy mans way of doing a "just get it working".