Solved

How to display month dd, yyyy time in c#

Posted on 2015-02-23
13
25 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
  • 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 42

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 32

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
 
LVL 49

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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 42

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 49

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 32

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

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…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

746 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now