Ordinal for DateTime.ToString()

  • Thread starter Thread starter Dylan Parry
  • Start date Start date
D

Dylan Parry

Hi folks,

I was wondering if there is any way of formatting a date to include the
ordinal characters? I've looked at the documentation for
DateTime.ToString(), but no where can I find information on ordinals.

For example, I currently use:

Console.WriteLine(myDate.ToString("d MMMM, yyyy"));

Which results in:

15 June, 2006

But I would like to have:

15th June, 2006

Am I overlooking anything? The only way I can think to do it is to use a
switch statement that takes the myDate.ToString("d") value as a
parameter and then returns the ordinal.

Any other ideas?

Thanks,
 
string strDate = dt.ToString("dd") + "th " + dt.ToString("MMMM ") +
dt.ToString("yy");
Console.WriteLine(strDate);


chanmm
 
chanmm said:
string strDate = dt.ToString("dd") + "th " + dt.ToString("MMMM ") +
dt.ToString("yy");
Console.WriteLine(strDate);

Which will instantly break if the ordinal isn't "th", eg. 1st, 2nd, 3rd
etc.
 
Dylan said:
Which will instantly break if the ordinal isn't "th", eg. 1st, 2nd, 3rd
etc.

do with an if...
if (dt.Day == 1) strdate = dt.ToString("dd") + "st";
else if (dt.Day == 2) strdate = dt.ToString("dd") + "nd";
else if (dt.Day == 3) strdate = dt.ToString("dd") + "rd";
else if (dt.Day > 3) strdate = dt.ToString("dd") + "th";
 
Alex said:
do with an if...
if (dt.Day == 1) strdate = dt.ToString("dd") + "st";
else if (dt.Day == 2) strdate = dt.ToString("dd") + "nd";
else if (dt.Day == 3) strdate = dt.ToString("dd") + "rd";
else if (dt.Day > 3) strdate = dt.ToString("dd") + "th";

You forgot some:

if (dt.Day == 1) strdate = dt.ToString("dd") + "st";
else if (dt.Day == 2) strdate = dt.ToString("dd") + "nd";
else if (dt.Day == 3) strdate = dt.ToString("dd") + "rd";
else if (dt.Day == 21) strdate = dt.ToString("dd") + "st";
else if (dt.Day == 22) strdate = dt.ToString("dd") + "nd";
else if (dt.Day == 23) strdate = dt.ToString("dd") + "rd";
else if (dt.Day == 31) strdate = dt.ToString("dd") + "st";
else strdate = dt.ToString("dd") + "th";
 
Hi folks,

I was wondering if there is any way of formatting a date to include the
ordinal characters? I've looked at the documentation for
DateTime.ToString(), but no where can I find information on ordinals.

For example, I currently use:

Console.WriteLine(myDate.ToString("d MMMM, yyyy"));

Which results in:

15 June, 2006

But I would like to have:

15th June, 2006

Am I overlooking anything? The only way I can think to do it is to use a
switch statement that takes the myDate.ToString("d") value as a
parameter and then returns the ordinal.

Any other ideas?

Thanks,

using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.OleDb;
using System.Windows.Forms;
using System.Messaging;
using System.IO;

namespace TestConsole
{
class Program
{
static void Main(string[] args)
{
for (int ndx = 0; ndx < 101; ndx++)
{
Console.WriteLine(ndx.ToString() + Ordinal(ndx));
}
Console.ReadLine();
}

private static string Ordinal(int number)
{
string strNum = number.ToString();
string ordinal = string.Empty;
if(strNum.EndsWith("0") ||
strNum.EndsWith("4") ||
strNum.EndsWith("5") ||
strNum.EndsWith("6") ||
strNum.EndsWith("7") ||
strNum.EndsWith("8") ||
strNum.EndsWith("9"))
{
ordinal = "th";
}
else if(strNum.EndsWith("1"))
{
ordinal = "st";
}
else if(strNum.EndsWith("2"))
{
ordinal = "nd";
}
else
{
ordinal = "rd";
}
return ordinal;
}
}
}

Good luck with your project,

Otis Mukinfus
http://www.arltex.com
http://www.tomchilders.com
 
Otis said:
private static string Ordinal(int number)
{
string strNum = number.ToString();
string ordinal = string.Empty;
if(strNum.EndsWith("0") ||
strNum.EndsWith("4") ||
strNum.EndsWith("5") ||
strNum.EndsWith("6") ||
strNum.EndsWith("7") ||
strNum.EndsWith("8") ||
strNum.EndsWith("9"))
{
ordinal = "th";
}
else if(strNum.EndsWith("1"))
{
ordinal = "st";
}
else if(strNum.EndsWith("2"))
{
ordinal = "nd";
}
else
{
ordinal = "rd";
}
return ordinal;

That works, except it will return "11st", "12nd", and "13rd". If there
is no built in formatting for the ordinal, I would do something like this:

static string ordinal(int input)
{
switch (input % 100)
{
case 11:
case 12:
case 13:
return "th";
default:
switch (input % 10)
{
case 1:
return "st";
case 2:
return "nd";
case 3:
return "rd";
default:
return "th";
}
}
}

That should handle the "special" cases of numbers ending in 11, 12, and 13.

Hope this helps.

Dan Manges
 
[snip]
That works, except it will return "11st", "12nd", and "13rd". If there
is no built in formatting for the ordinal, I would do something like this:

static string ordinal(int input)
{
switch (input % 100)
{
case 11:
case 12:
case 13:
return "th";
default:
switch (input % 10)
{
case 1:
return "st";
case 2:
return "nd";
case 3:
return "rd";
default:
return "th";
}
}
}

That should handle the "special" cases of numbers ending in 11, 12, and 13.

Hope this helps.

Dan Manges

Good eyes Dan! I didn't notice that when I looked at the output :o(
Good luck with your project,

Otis Mukinfus
http://www.arltex.com
http://www.tomchilders.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top