help a newbie?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

trying to dislplay day of the month and the name of the month (current) from
user system. Using Visual Studio .Net 2003 in C#

On this 4th day of September 2004

I understand how to get system DateTime but not how to get above.

I really appreciate evereyone in this discussion group, I have learned alot
from all of you
 
Name of the month is a sadly neglected item in the .Net framework. I
usually create an enum with all the months and use it to get the name of the
month from DateTime.Month. Of course, converting the date to your format, 4
for 4th, is going to just take some C# coding. There's not going to be any
shortcuts for that.

Dale Preston
MCAD, MCDBA, MCSE
 
Thanks Dale

Dale Preston said:
Name of the month is a sadly neglected item in the .Net framework. I
usually create an enum with all the months and use it to get the name of the
month from DateTime.Month. Of course, converting the date to your format, 4
for 4th, is going to just take some C# coding. There's not going to be any
shortcuts for that.

Dale Preston
MCAD, MCDBA, MCSE
 
Isn't this a bit simpler?

static void Main(string[] args)
{
DateTime d = DateTime.Now;
Console.WriteLine("On this {0}th day of {1} {2}", d.Day, d.ToString("MMMM"), d.Year);
}

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

Name of the month is a sadly neglected item in the .Net framework. I
usually create an enum with all the months and use it to get the name of the
month from DateTime.Month. Of course, converting the date to your format, 4
for 4th, is going to just take some C# coding. There's not going to be any
shortcuts for that.

Dale Preston
MCAD, MCDBA, MCSE
 
trying to dislplay day of the month and the name of the month
(current) from user system. Using Visual Studio .Net 2003 in C#

On this 4th day of September 2004

I understand how to get system DateTime but not how to get
above.

I really appreciate evereyone in this discussion group, I have
learned alot from all of you

The DateTime.ToString() method is very flexible:

DateTime dt = new DateTime(2004, 9, 4);
string s = "On this " + dt.ToString(@"d\t\h \d\a\y \o\f MMMM yyyy");
 
Richard,
Thank you for your assistance... I am unclear as how to add your code. This
is what I have now.....

public class date1 : System.Web.UI.UserControl
{
protected System.Web.UI.WebControls.Label DateLabel1;
protected System.Web.UI.WebControls.Label DateLabel;

private void Page_Load(object sender, System.EventArgs e)
{

this.LoadInfo();
}

private void LoadInfo()
{
this.DateLabel1.Text = System.DateTime.Now.ToString();

}
 
This should do it

private void LoadInfo()
{
DateTime d = DateTime.Now;
this.DateLabel1.Text = string.Format("On this {0}{1} day of {2} {3}", d.Day, GetDaySuffix(d.Day), d.ToString("MMMM"), d.Year);
}

private string GetDaySuffix(int day)
{
string suffix = null;
switch(day)
{
case 1:
case 31:
suffix = "st";
break;
case 2:
case 22:
suffix = "nd";
break;
case 3:
suffix = "rd";
break;
default:
suffix = "th";
break;
}
return suffix;
}

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

Richard,
Thank you for your assistance... I am unclear as how to add your code. This
is what I have now.....
 
Thank you very much it works wonderfully. I shall take some time to
understand how it functions. Thanks for giving your valuable time.
 
You will have to use a switch (or if block) to get best results unless
you want to append 'th' to each day. I'd do something like this
System.Text.StringBuilder str = new System.Text.StringBuilder();
str.AppendFormat("On this {0}", dt.Day);
switch (dt.Day)
{
case 1:
case 21:
case 31:
str.Append("st");
break;
case 2:
case 22:
str.Append("nd");
break;
case 3:
case 23:
str.Append("rd");
break;
default:
str.Append("th");
break;
}
str.AppendFormat(" day of {0:MMMM} {1}", dt, dt.Year);

Ron Allen
 
Thanks Ron

Ron Allen said:
You will have to use a switch (or if block) to get best results unless
you want to append 'th' to each day. I'd do something like this
System.Text.StringBuilder str = new System.Text.StringBuilder();
str.AppendFormat("On this {0}", dt.Day);
switch (dt.Day)
{
case 1:
case 21:
case 31:
str.Append("st");
break;
case 2:
case 22:
str.Append("nd");
break;
case 3:
case 23:
str.Append("rd");
break;
default:
str.Append("th");
break;
}
str.AppendFormat(" day of {0:MMMM} {1}", dt, dt.Year);

Ron Allen
 
sorry Richard I don't yet understand the switch.... this is what I have

case 1:
case 31:
suffix = "st";
break;
case 2:
case 22:
suffix = "nd";
break;
case 3:
suffix = "rd";
break;
default:
suffix = "th";
break;
 
is this correct?

string suffix = null;
switch(day)
{
case 1:
case 31:
suffix = "st";
break;
case 2:
case:21
case 22:
suffix = "nd";
break;
case 3:
case:23
suffix = "rd";
break;
default:
suffix = "th";
break;
}
return suffix;
}
 
corrected (i think)
string suffix = null;
switch(day)
{
case 1:
case:21
case 31:
suffix = "st";
break;
case 2:
case 22:
suffix = "nd";
break;
case 3:
case:23
suffix = "rd";
break;
default:
suffix = "th";
break;
}
return suffix;
}
 
You want to print out the "4th day of ...". The point is that the "th" is only valid for certain days of the month. e.g: 21st, 3rd, 22nd use different suffixes. The switch is calculating the appropriate suffix for the day of the month and I missed a couple that should have not been "th" which is the default in the switch. The full switch statement should be:

case 1:
case 21:
case 31:
suffix = "st";
break;
case 2:
case 22:
suffix = "nd";
break;
case 3:
case 23:
suffix = "rd";
break;
default:
suffix = "th";
break;

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

sorry Richard I don't yet understand the switch.... this is what I have

case 1:
case 31:
suffix = "st";
break;
case 2:
case 22:
suffix = "nd";
break;
case 3:
suffix = "rd";
break;
default:
suffix = "th";
break;
 
Back
Top