DateTime AddMinutes method not working

后端 未结 6 832
挽巷
挽巷 2021-02-07 23:50

The purpose of my method is to get the currentTime, and set it back for 20 minutes. For what I can see, my method is correct, but the output shows something else.

This

相关标签:
6条回答
  • 2021-02-08 00:23
    currentTime = DateTime.Now.AddMinutes(5.0f);
    
    0 讨论(0)
  • 2021-02-08 00:31

    Description

    The AddMinutes function returns a DateTime.

    DateTime.AddMinutes Method Returns a new DateTime that adds the specified number of minutes to the value of this instance.

    Sample

    DateTime currentTime = DateTime.Now;
    double minuts = -20;
    currentTime = currentTime.AddMinutes(minuts);
    
    Console.WriteLine("Nuværende tid: "+currentTime);
    

    More Information

    • DateTime.AddMinutes Method
    0 讨论(0)
  • 2021-02-08 00:31

    try:

    currentTime = currentTime.AddMinutes(minuts);
    
    0 讨论(0)
  • 2021-02-08 00:33

    DateTime is "immutable", what that means is you can never modify an existing instance, only make new ones. Strings are the same, for example. So you need to use the result of the AddMinutes call, which gives you your existing currentTime with the minuts variable applied.

    currentTime = currentTime.AddMinutes(minuts);
    
    0 讨论(0)
  • 2021-02-08 00:39
    ...
    currentTime = currentTime.AddMinutes(minuts); 
    ...
    
    0 讨论(0)
  • 2021-02-08 00:42

    AddMinutes returns a new DateTime object so you need:

        DateTime currentTime = DateTime.Now;
        double minuts = -20;
        DateTime newTime = currentTime.AddMinutes(minuts);
    
        Console.WriteLine("Nuværende tid: "+newTime);
    
    0 讨论(0)
提交回复
热议问题