0

I am trying to remove the time zone from a string.

The string looks like below.

2/05/2018 6:54:00 AM

This data has been retrieved from a local database.

Code Part:

 string datCommisioned = (rdr.GetValue(4).ToString());
 string dateonly = datCommisioned.ToString("dd/MM/yyyy");

When I tried to execute the above coding it's getting an error,

cannot convert from 'string' to 'System.IFormatProvider'

Any idea how to catch the date part excluding the time? Thanks!

Edit: This is probably not a duplicate question as I refer the other questions before posting. The context of this question is different because the string I'm extracting is not recognised as a valid DateTime (Manually entered by users) thus DateTime class cannot be used.

  • `datCommisioned` is string And You are trying to convert it to String again. String.ToString() accepts IFormatProvider. – Chetan May 02 '18 at 00:54
  • 2
    Possible duplicate of [Reading a date using DataReader](https://stackoverflow.com/questions/5619216/reading-a-date-using-datareader) – Chetan May 02 '18 at 00:55
  • Look here https://stackoverflow.com/questions/5619216/reading-a-date-using-datareader – Chetan May 02 '18 at 00:55

4 Answers4

2
//Assuming value at index 4 is a DateTime.
string datCommisioned = rdr.GetDateTime(4).ToString(@"dd/MM/yyyy");

It's good practice to use the more specific and strongly typed methods with a DataReader. Helps identify bugs faster.

Zer0
  • 7,191
  • 1
  • 20
  • 34
  • why the '@' sign? – pm100 May 02 '18 at 01:17
  • 1
    @pm100 https://stackoverflow.com/questions/3311988/what-is-the-difference-between-a-regular-string-and-a-verbatim-string – Daniel Mann May 02 '18 at 01:21
  • @pm100 to avoid escapes ('\') should they be added. Habit of mine but is not necessary in the example. Out of instinct I avoid escapes in format strings. – Zer0 May 02 '18 at 01:24
1

There are several approaches

  1. Use DateTime object.

    DateTime dateTime = DateTime.Parse(datCommissioned);
    string dateonly = dateTime.ToString("dd/MM/yyyy");
    
  2. Split on whitespace

    var split = datCommissioned.Split(' ');
    string dateonly = split[0];
    
Camilo Terevinto
  • 31,141
  • 6
  • 88
  • 120
Tim
  • 2,089
  • 1
  • 12
  • 21
  • 1
    I wouldn't suggest a parse when you can get the DateTime directly from the DataReader – Camilo Terevinto May 02 '18 at 01:03
  • @CamiloTerevinto nowhere does it state that he is using a `DataReader`, although I see from the abbreviation why that is being assumed. – Tim May 02 '18 at 01:04
  • Yeah as Camilo mentioned I'm using a data reader object to read the data from a local database. This data has been inserted manually by users. There's an exception when I use DateTime class. System.FormatException: 'String was not recognized as a valid DateTime.' I recon the reason for this issue is because data was entered manually by users. @Tim – matt daemon May 02 '18 at 01:11
  • @mattdaemon lots of ways around that. Put a `try` around the `Parse`, or somehow ensure the data is correct before parsing. If it is saved/retrieved as a string, the other answer here won't work. – Tim May 02 '18 at 01:12
0

You just need to change it to be this.

var datCommisioned = DateTime.parse(rdr.GetValue(4)).ToString("dd/MM/yyyy");

You are currently converting it to a string then trying to ToString() the String, when you just need to toString() the date from the reader.

Joe Hartzell
  • 357
  • 3
  • 11
0

Just another approach here; You can utilize DateTime.Parse() or perhaps a safer alternative, DateTime.TryParse(), to convert the string to a DateTime object. Then, use the Date property of the DateTime object to basically set the time to midnight, ie. 00:00:00. Depending on what you’re using the date for, having the DateTime object instead of a string could be helpful. You can then call ToString(“d”) or use another formatter to give you the date only portion in the desired format.

if (TryParse(datCommissioned, out DateTime commissionedDate))
{
       commisionedDate = commisionedDate.Date;
       string commissionedDateText = commissionedDate.ToString(“dd/MM/yyyy”);
}
iCode
  • 1,254
  • 1
  • 13
  • 16