0

I have a variable openDate which holds date and time, and I would like to strip just the date. I tried the below example and it is not working. What am I doing wrong, or rather how should I do it because the variable openDate remains the same even after trying to strip just the date? The value of openDate is "2012-03-08 00:00:00"

openDate = ! string.IsNullOrEmpty(node.ChildNodes[f].Attributes["ows_PMO_x0020_Origination_x0020_Date"].Value)
           ? node.ChildNodes[f].Attributes["ows_PMO_x0020_Origination_x0020_Date"].Value
           : "" ;
openDate  = String.Format("{0:MM/dd/yyyy}", openDate);
Paul Roub
  • 35,100
  • 27
  • 72
  • 83
john
  • 15
  • 4
  • Or you can just use `openDate.ToString("MM/dd/yyyy");` – Chibueze Opata Jul 28 '16 at 16:30
  • Possible duplicate of [How to remove time portion of date in C# in DateTime object only?](http://stackoverflow.com/questions/6121271/how-to-remove-time-portion-of-date-in-c-sharp-in-datetime-object-only) – devlin carnate Jul 28 '16 at 16:38

6 Answers6

1

considering openDate is of a String type, i would do this

var dt = DateTime.Parse(openDate).ToString("MM/dd/yyyy");

din
  • 586
  • 2
  • 6
  • 22
1

From your code it is clear that openDate is of type string and you have value that is a string representation of DateTime, you can apply DateTime formatting on string values.

You have multiple options.

  • Convert string openDate to a DateTime value and then apply formatting
  • Do some string operations to extract the date part from your string value.

String operations:

string openDate = "2012-03-08 00:00:00";
string formatted = openDate.Substring(0, openDate.IndexOf(' '));

DateTime Parsing.

DateTime parsedDateTime = DateTime.Parse(openDate);
string formattedDateTime = parsedDateTime.ToString("MM/dd/yyyy", CultureInfo.InvariantCulture);
Habib
  • 205,061
  • 27
  • 376
  • 407
0

You need to convert your date into a DateTime object first. See examples here if your string is in a different or custom format.

openDate = !string.IsNullOrEmpty(node.ChildNodes[f].Attributes["ows_PMO_x0020_Origination_x0020_Date"].Value)? node.ChildNodes[f].Attributes["ows_PMO_x0020_Origination_x0020_Date"].Value: "" ;
//openDate is a string at this point. You'll need to convert it to a datetime object first, for the following line to work:
var dtObject = DateTime.Parse(openDate);
//Format the newly created datetime object
openDate  = String.Format("{0:MM/dd/yyyy}", dtObject);
Goodbye StackExchange
  • 21,680
  • 7
  • 47
  • 83
0

You can format datetime using: If it is a datetime:

OpenDate = OpenDate.ToString("yyyy-mm-dd");

If the datatype is not datetime and you are sure the format will always be that then you can always convcert the string to datetime and use the method described above.

Convert.ToDateTime(openDate).ToString("yyyy-mm-dd");
Abhinandan
  • 281
  • 5
  • 13
0

The answers are great, especially if you would like to control the format of your 'time' part. Here is teh simplest way to get what you are after:

        var dt = Convert.ToDateTime("2012-03-08 00:00:04");

        Console.WriteLine(dt.ToLongTimeString());
        Console.WriteLine(dt.TimeOfDay);

Output:

enter image description here

lucas
  • 3,457
  • 6
  • 23
  • 42
-1

Use the following - openDate = openDate.Date

Ed W
  • 115
  • 3
  • 18