1

I have a SQL output that comes with different fields. A couple of those fields are dates and I want to convert them to timestamp. Here is how the output of the dates look as it comes from the database:

4/6/2016 9:00:00 PM

And when converted to JSON it looks like this:

opening_date":"4/6/2016 9:00:00 PM"

I tried the following to convert the SQL request output

meeting_start_date = DateTime.ParseExact((string)((object[])((ArrayList)results)[i])[3].ToString(), "M/d/yyyy h:mm:ss tt", CultureInfo.InvariantCulture)

as per what I found in here, but I get the following error

Cannot implicitly convert type 'System.DateTime' to 'string'

is there something wrong with how I am using the function, or is there a better way to make the conversion from the SQL output to timestamp so it can be added to the json output.

Community
  • 1
  • 1
Tavo
  • 173
  • 1
  • 15
  • Is `meeting_start_date` a string? Otherwise, what exactly is the type of `results`? – wimh Apr 06 '16 at 15:36
  • @Wimmel the `meeting_start_date` should be a string but it is originally a `datetime` in sql – Tavo Apr 06 '16 at 15:39
  • If you need a string, why do you convert it into a DateTime first? The output from the database looks identical to the format you need in the json. – wimh Apr 06 '16 at 15:43
  • Why should it be a string? If it's a datetime, keep it as a datetime object – Siyual Apr 06 '16 at 15:43
  • If you model holds DateTime property, it'll be serialized just fine using e.g. JSON.NET – Mikko Viitala Apr 06 '16 at 15:44
  • I need it as a string but I want it to convert it to epoch i.e. `4/6/2016 9:00:00 PM` to `1459947600000` – Tavo Apr 06 '16 at 15:46
  • http://stackoverflow.com/questions/9453101/how-do-i-get-epoch-time-in-c – Kateract Apr 06 '16 at 15:53

1 Answers1

0

What you need to do to fix this error is after parsing your DateTime, end it with a ToString(). This will allow you to assign the value to a string variable like so

meeting_start_date = DateTime.ParseExact((string)((object[])((ArrayList)results)[i])[3].ToString(), "M/d/yyyy h:mm:ss tt", CultureInfo.InvariantCulture).ToString();

As a side note, in this part (string)((object[])((ArrayList)results)[i])[3].ToString() you are casting the value as a string even though you used ToString() on it already

Alfie Goodacre
  • 2,753
  • 1
  • 13
  • 26