标签云

微信群

扫码加入我们

WeChat QR Code

Is it possible to include the day suffix when formatting a date using DateTime.ToString()?For example I would like to print the date in the following format - Monday 27th July 2009. However the closest example I can find using DateTime.ToString() is Monday 27 July 2009.Can I do this with DateTime.ToString() or am I going to have to fall back to my own code?


Did someone say NodaTime?

2019年04月25日13分20秒

FYI, "[date] ordinal suffix" is what these are called. "Day" typically refers to Monday-Sunday

2019年04月25日13分20秒

This would need to be further expanded to cover the other cases, otherwise you'll end up with "21th", for example.

2019年04月25日13分20秒

This should solve your problem perfectly now

2019年04月25日13分20秒

+1 for the link to string formatting patterns

2019年04月25日13分20秒

For what it's worth, Microsoft's official documentation of string formatting options can be found here.

2019年04月25日13分20秒

DateTime.Now is retrieved several times in the same expression, the values can be different if the code is executed around midnight.

2019年04月25日13分20秒

+1 Simple, easy to read, and most importantly, works for all cases.

2019年04月25日13分20秒

Lazlow In case you are wondering about the sudden activity: your answer was linked as an "how to do it right" example by The Daily WTF.

2019年04月25日13分20秒

tobias_k thanks - I wondered how my minuscule reputation had doubled so quickly!

2019年04月25日13分20秒

This should be the accepted answer, thank you

2019年04月25日13分20秒

Thanks Buddy. Much Helpful. I have implemented in my project. :)

2019年04月25日13分20秒

Your code kind of... forgets to check the useExtendedSpecifiers boolean :p

2019年04月25日13分20秒

The most elegant solution. This is exactly what extension methods are designed for. Have added to my ever growing extension method library, thanks!

2019年04月25日13分20秒

The eightth is wrong...

2019年04月25日13分20秒

Greg That's strange as var suffix = CreateDateSuffix(new DateTime(2013, 10, 8)); returns '8th' in my case?

2019年04月25日13分20秒

If it were appending 'th' to the string 'eight' it would be wrong, but in this case since you used the digit 8 it is correct.

2019年04月25日13分20秒

The above method as it stands takes a DateTime object and I cannot see how it could be instantiated with anything other than numeric values - in this case '8' representing the day of the month.

2019年04月25日13分20秒

In that case, it's correct. If you were to, I don't know, replace the numeric value with the textual representation of the number, it would be wrong. I suppose that's up to whoever is doing the replacing though, to know this and replace '8t' with 'eight', or more correctly, replace '8' with 'eigh'.

2019年04月25日13分20秒

and what about "11th", "12th" and "13th"?

2019年04月25日13分20秒

I must have missed this. I fixed that above.

2019年04月25日13分20秒

FYI if this was needed for numbers greater than ("dd") would produce, use string.PadLeft()

2019年04月25日13分20秒

PiotrLewandowski - You not from Manchester by any chance? Cause I know one from there. Too freaky+1 btw

2019年04月25日13分20秒

+ 1, works well

2019年04月25日13分20秒

Surprised that this hasn't got more up votes, I prefer the fact that it's an extension. Makes it much easier to use and arguably more readable.

2019年04月25日13分20秒

Although this is a more general purpose method, for any number, not just month days (I think).

2019年04月25日13分20秒

This version shows only the first digit of a day ie 1st March 2017 where I didn't want the day name first as in a long date and didn't want the 01st instead of 1st

2019年04月25日13分20秒

This gives 11st, 12nd, and 13rd...

2019年04月25日13分20秒

Thanks AakashM your right, I've editted to correct the mistake.

2019年04月25日13分20秒

Now it gives 1th, 2th, and 3th.

2019年04月25日13分20秒