-2

I am trying to convert 2014-12-25 (yyyy-mm-dd), to December 25, 2014. What is the best practice on how to do this? I currently have it stored in a variable because they are choosing the date from a date picker, but I am displaying that value somewhere else on the page and want it in the above format.

Code:

myLabel.Text = "Period: " + FromDate + " to " + ToDate;

The from and to date are the two variables holding the date from the datepicker. I need help on converting it to December 25, 2014

Hamid Pourjam
  • 20,441
  • 9
  • 58
  • 74
codeFinatic
  • 171
  • 1
  • 5
  • 18
  • 1
    Are your variables string or DateTime? – Mihai Caracostea Feb 17 '15 at 23:39
  • they are string @MihaiCaracostea – codeFinatic Feb 17 '15 at 23:42
  • The goal of this was actually to see how many down votes a question could get. Doing good so far @JeremyThompson – codeFinatic Feb 17 '15 at 23:53
  • _I want a [packet of Tim Tams that never runs out](https://www.youtube.com/watch?v=KoggSVxghWs)_ . Welcome to SO. Please don't post requirements and like Tim Tims, expect code to magically appear. Any code to show? What research have you done? Good luck! (the code you are showing at the moment has nothing to do with conversion so it arguably does not count) –  Feb 18 '15 at 00:36

4 Answers4

11
thisDate1.ToString("MMMM dd, yyyy")

Edit:

Since your variables are string:

DateTime.ParseExact(stringDate, "yyyy-MM-dd", CultureInfo.InvariantCulture).ToString("MMMM dd, yyyy");

That would give you the conversion of formats from your string datetime to the one you want.

Mihai Caracostea
  • 8,336
  • 4
  • 27
  • 46
  • so i could do, FromDate.ToString("MMMM dd, yyyy") ? FromDate is already is String. I don't think it will like that – codeFinatic Feb 17 '15 at 23:41
3
Console.WriteLine("Today is " + thisDate1.ToString("MMMM dd, yyyy") + ".");

See "Custom Date and Time Format Strings"

John Saunders
  • 160,644
  • 26
  • 247
  • 397
AnotherDeveloper
  • 1,242
  • 1
  • 15
  • 36
1
myLabel.Text = "Period: " + FromDate.ToString("MMMM dd, yyyy") + " to " + ToDate.ToString("MMMM dd, yyyy"); 
Hamid Pourjam
  • 20,441
  • 9
  • 58
  • 74
Paul McLean
  • 3,450
  • 6
  • 26
  • 36
1

if you have "2014-12-25" in string variable you should parse it first and convert it to a DateTime

 var dt = DateTime.ParseExact(FromDate, "yyyy-MM-dd", CultureInfo.InvariantCulture);
 Console.WriteLine(dt.ToString("MMMM dd, yyyy"));

but if you have "2014-12-25" already in a DateTime variable it is just a matter of formatting. just use

 Console.WriteLine(FromDate.ToString("MMMM dd, yyyy"));

list of date time formats

Hamid Pourjam
  • 20,441
  • 9
  • 58
  • 74