473,320 Members | 1,865 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Ordinal for DateTime.ToString()

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,

--
Dylan Parry
http://electricfreedom.org -- Where the Music Progressively Rocks!
Jun 15 '06 #1
7 7905
string strDate = dt.ToString("dd") + "th " + dt.ToString("MMMM ") +
dt.ToString("yy");
Console.WriteLine(strDate);
chanmm

"Dylan Parry" <us****@dylanparry.com> wrote in message
news:1l***************@dylanparry.com...
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,

--
Dylan Parry
http://electricfreedom.org -- Where the Music Progressively Rocks!

Jun 15 '06 #2
chanmm wrote:
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 Parry
http://electricfreedom.org -- Where the Music Progressively Rocks!
Jun 15 '06 #3

Dylan Parry wrote:
chanmm wrote:
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.


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

Jun 15 '06 #4
Alex wrote:
Dylan Parry wrote:
chanmm wrote:
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.


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";
Jun 15 '06 #5
On Thu, 15 Jun 2006 13:07:03 +0100, Dylan Parry <us****@dylanparry.com> wrote:
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
Jun 16 '06 #6
Otis Mukinfus wrote:
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
Jun 16 '06 #7
On Fri, 16 Jun 2006 04:13:17 GMT, Dan Manges <da***********@gmail.com> wrote:

[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
Jun 16 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Robert Misiak | last post by:
Hello- I live in the US, however I'm attempting to make a product of mine more international-friendly. There are a number of instances where a calendar function of my program displays various...
15
by: Fritz Switzer | last post by:
I'd like to have a string assigned the value of a DateTime.AddMinutes(amount) so that the string is formatted in "HH:MM" format. For example: DateTime.Now.AddMinutes(30) returns "00:30" ...
7
by: .Net Sports | last post by:
Before processing my data in a datagrid, I need to parse the day of the week (which will be my 'rqsday' variable) from a string that comes over on a querystring: string rqs =...
6
by: Ante Perkovic | last post by:
Hi, How to declare datetime object and set it to my birthday, first or last day of this month or any other date. I can't find any examples in VS.NET help! BTW, what is the difference...
38
by: nobody | last post by:
I know that given a FormatString and a DateTime you can use DateTime.ToString(...) to convert the DateTime to a String. My question is how can you turn that around? Given a String and a...
5
by: ns21 | last post by:
Our application is windows desktop application. We are using VS.Net 2003, C#, Framework 1.1, SQL 2000. We use webservices to add/update/select objects. We are using XML Serialization. Following is...
6
by: Jake K | last post by:
string var = DateTime.Today.Month.ToString() + "-" + DateTime.Today.Day.ToString() + "-" + DateTime.Today.Year.ToString() + "-" + DateTime.Today.Hour.ToString() + "-" +...
4
by: Bill Gower | last post by:
Why won't this work? What do I need to do to make it work? DateTime? DateMember; if((DateTime.Parse(oldRow.ToString) == null)) DateMember = null; else DateMember =...
9
by: David Jackson | last post by:
Hello, Is there anything in the framework which will format a date to show the ordinal representation of the day value e.g. 28th June 2007 1st August 2007 instead of
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.