简体   繁体   中英

cannot resolve symbol when adding minutes into date time in c#?

I have issue that I cant solve. I am trying to add minutes and subtract minutes. basically be in that 30 minutes block range. However, in my code below I am trying to get my key value and add 30 minutes but for some reason AddMinutes gives an error:

Cannot resolve symbol AddMinutes.

here is my code:

                  var results = JsonConvert.DeserializeObject<dynamic>(barCodeValue);
              var gettingTheName = (string) results.Evaluation.Value;
              TextHeader.text = gettingTheName;
              var qrCodeString = $"https://**************.firebaseio.com/Evaluations/.json?orderBy=\"$key\"&startAt=\"{gettingTheName}\"&limitToFirst=1";
              var matchingsLink = new WebClient().DownloadString(qrCodeString);
              var objs = JObject.Parse(matchingsLink);
              var someId = objs.First.First["Active"].ToString();
              var data = objs[gettingTheName];

              try
               {

                    if (!((bool)data["Active"] == false && (bool)data["Completed"] &&    
                        DateTime.Now < data["ScheduleStartTime"].AddMinutes(30) &&
                        DateTime.Now > data["ScheduleStartTime"].AddMinutes(-30)))  

You are using AddMinutes() method to the JToken type object. AddMinutes only works with DateTime.

DateTime.Parse("dateInStringFormat") will parse the string to DateTime format.. to which you can then add Minutes to.

   DateTime.Now < DateTime.Parse(data["ScheduleStartTime"].ToString()).AddMinutes(30) &&
       DateTime.Now > DateTime.Parse(data["ScheduleStartTime"].StoString()).AddMinutes(-30))) 

This is probably because data["ScheduleStartTime"] is not an instance of DateTime , since JSON has no built in representation of dates or times. What is most likely is data["ScheduleStartTime"] is an ISO 8601 date represented as a string, in which case you should parse it before comparing it to DateTime.Now :

// ...

var scheduleStartTime = DateTime.Parse(data["ScheduleStartTime"].ToString());

if (!((bool)data["Active"] == false && (bool)data["Completed"] &&    
                        DateTime.Now < scheduleStartTime.AddMinutes(30) &&
                        DateTime.Now > scheduleStartTime.AddMinutes(-30)))

// ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM