Solved

How to display month dd, yyyy time in c#

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

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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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
 
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 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 33

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SqlDependency to get update from sql to my c# app 2 66
PrintingFoundIt(Almost!(Damn!)) 1 31
Hey!! 5 37
Firing an ASP.NET event on selection of a JQuery datepicker 4 31
Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

830 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