Error when converting String to DateTime: System.FormatException: String was not recognized as a valid DateTime

245 views Asked by At

I am trying to compare two dates in C#. One is a sunset date and one is the current date. I am trying to determine whether it's after or before sunset. I have two dates like that:

  1. 2021-12-02 16:19 (Current Date)
  2. 05:55:50 PM (Sunset Date)

I have been trying to compare them this way:

var currentTime = DateTime.ParseExact(Weather.location.localtime, "yyyy-MM-dd HH:mm", CultureInfo.InvariantCulture).TimeOfDay;
var sunsetTime = DateTime.ParseExact(SunriseSunset.results.sunset, "hh:mm:ss tt", CultureInfo.InvariantCulture).TimeOfDay;

bool sunStillUp = currentTime < sunsetTime;

I know that the error means that the given dates are in incorrect format, however I cannot see how..

Weather.location.localtime is this (getting data from an api):

enter image description here

And SunriseSunset.results.sunset is this (getting data from an api):

enter image description here

0

There are 0 answers