user3272721
user3272721

Reputation: 13

Current date time with specific output format

I need to produce the current date to appear as such "February 24,2014" given these guidelines:

When the form/page loads:

  1. Create a new instance of the DateTime object
  2. Set the created DateTime instance to the current date time (use your instance name "=Date.Time();")

Code:

protected void Page_Load(object sender, EventArgs e)
{
    DateTime currentDateTime = Date.Time();
    System.Console.WriteLine(currentDateTime.ToString("MMMMMMMMM dd, yyyy"));
    TodayIsLabel.Text = ("Today is: " + currentDateTime);
}

I'm getting an error stating

The name 'Date' does not exist in current context

I also tried using DateTime.Now but it didn't convert to the required format that is being asked. It also seems being required to use "=Date.Time();" is not a valid format so I'm not sure if its a typo or not.

Upvotes: 0

Views: 135

Answers (1)

Derek
Derek

Reputation: 8763

      DateTime currentDateTime = DateTime.Now;
      System.Console.WriteLine( currentDateTime.ToString( "MMMM dd, yyyy" );

You will also want:

TodayIsLabel.Text = "Today is: " + currentDateTime.ToString( "MMMM dd, yyyy" );

To see the full list of formats look here:

http://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110).aspx

MMMM - The full name of the month.

There is no such thing where you put more than four M's (one for each letter in the month name). That wouldn't work because you don't always know how many letters are in the current month name.

Upvotes: 3

Related Questions