Solved

How to display month dd, yyyy time in c#

Posted on 2015-02-23
13
35 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 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

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 describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…

734 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