2

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 ");
Sora
  • 2,465
  • 18
  • 73
  • 146

8 Answers8

3

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.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
  • Parse doesn't have the same format as ToString(), the former is richer. – Oscar Nov 29 '13 at 09:17
  • I read the question as asking how to format and existing DateTime in this way, not how to parse an existing string into a DateTime. This answer currently shows parsing. – chwarr Nov 29 '13 at 09:21
  • i found the solution and it's : DateTime.Now.toString("dddd MMM dd'th' yyyy") thank you – Sora Nov 29 '13 at 09:22
  • @Sora But this works only in this case. When your day will be `1` for example, you get `1th` instead of `1st`. – Soner Gönül Nov 29 '13 at 09:26
  • @Sora, `"dddd MMM dd'th' yyyy"` will output the wrong value for 2013-12-01. On that day, you'll want to output "Sunday Dec 1*st* 2013" not "Sunday Dec 1*th* 2013". [Ordinal numbers](http://en.wikipedia.org/wiki/English_numerals#Ordinal_numbers) don't always end with "th". – chwarr Nov 29 '13 at 09:26
  • @Downvoter care to comment at least so I can see where I might be wrong? – Soner Gönül Nov 29 '13 at 09:36
  • aren't there a global way to do this ? or i should write a hard code on my own to globalize it – Sora Nov 29 '13 at 11:00
1

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"

Ramashankar
  • 1,598
  • 10
  • 14
0

D? F? It depends what kind of format you want! http://msdn.microsoft.com/en-us/library/az4se3k1(v=vs.110).aspx

Oscar
  • 13,594
  • 8
  • 47
  • 75
0

Try

    DateTime now = DateTime.Now;
    Console.WriteLine(now.ToString("D"));
MusicLovingIndianGirl
  • 5,909
  • 9
  • 34
  • 65
0

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.

Community
  • 1
  • 1
chwarr
  • 6,777
  • 1
  • 30
  • 57
0

try

    DateTime.Now.ToString("dddd MMM dd'th' yyyy");
Cris
  • 12,799
  • 5
  • 35
  • 50
0

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
User2012384
  • 4,769
  • 16
  • 70
  • 106
-1

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".