Solved

How to display month dd, yyyy time in c#

Posted on 2015-02-23
13
34 Views
Last Modified: 2016-06-22
I am trying to display time in The format 'Month dd yyyy' but for some reason I am unable to.  It would have to follow Convert.ToDateTime.  How do I get it in the format I want of 'Month dd yyyy'?
0
Comment
Question by:VBBRett
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +2
13 Comments
 
LVL 44

Accepted Solution

by:
AndyAinscow earned 500 total points
ID: 40625547
String s = DateTime.Now.ToString("MMMM dd yyyy");

s is "February 23 2015"
0
 
LVL 43

Expert Comment

by:Chris Stanyon
ID: 40625556
Have a look at this:

DateTime yourDate = Convert.ToDateTime(yourDateString);
console.writeline( yourDate.ToString("MMMM dd yyyy") );

Open in new window

1
 
LVL 34

Expert Comment

by:it_saige
ID: 40625747
@Chris - A better solution is to ensure that the string is a date time by using TryParse:
using System;

namespace EE_Q28622397
{
	class Program
	{
		static void Main(string[] args)
		{
			string yourDateString = "12/20/2003";
			DateTime yourDate = DateTime.MinValue;
			if (DateTime.TryParse(yourDateString, out yourDate))
				Console.WriteLine(yourDate.ToString("MMMM dd, yyyy"));
			else
				Console.WriteLine("The string entered does not represent a valid date.");
			Console.ReadLine();
		}
	}
}

Open in new window

Which produces the following output -Capture.JPG-saige-
0
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
LVL 50

Expert Comment

by:Gustav Brock
ID: 40627699
You can use Parse:

String textDate = "12 Oct 2015";
String formattedDate = DateTime.Parse(textDate).ToString("MMMMM dd, yyyy");
Console.WriteLine (formattedDate);

October 12, 2015

/gustav
0
 

Author Comment

by:VBBRett
ID: 40628030
Thank you!  Will these solutions also include the time?
0
 
LVL 43

Expert Comment

by:Chris Stanyon
ID: 40628044
No - you asked for it to be displayed as Month Day Year, which is what we've offered. If you want the time, you'd need to include that in the format string:

// for 24 hour time
yourDate.ToString("MMMM dd yyyy HH:mm");

// for 12 hour time with (am/pm)
yourDate.ToString("MMMM dd yyyy hh:mm tt");
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 40628045
To repeat my very first comment:
String s = DateTime.Now.ToString("MMMM dd yyyy");

s is "February 23 2015"

So no, the time will not appear.

If you want the time then you need to specify that as well.  eg.
String s = DateTime.Now.ToString("MMMM dd yyyy  hh.mm.ss");
s is now February 24 2015  14.11.13

ps.  Look up the ToString function in the help file for all of the possible flags you can use to control the output of the date time.
0
 
LVL 50

Expert Comment

by:Gustav Brock
ID: 40628046
Yes and no. There is no time from, say, "12 Oct 2015" but if you apply a time format, it will show 00:00 or similar.

/gustav
0
 

Author Comment

by:VBBRett
ID: 40656880
I will give all of these suggestions a try and I will check back soon.  Thank you everyone.
0
 
LVL 34

Expert Comment

by:it_saige
ID: 41664984
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 41665095
#a40625547 does what was asked for in formatting a time to display Month dd yyyy
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

737 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question