Solved

How to display month dd, yyyy time in c#

Posted on 2015-02-23
13
36 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 51

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 51

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

Industry Leaders: 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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

626 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