locked
time difference in c# RRS feed

  • Question

  • User639567535 posted

    scenario is there is a drive and in that drive there is a files which is automatically created in every 4 mints so i fetch this date time in in this code 

    now i want to insert data in db that when any file not created within 4 mint then i want to insert data in db 

    data is sent to db but problem is this data not send when 4 mint is over and i want this condition

    code

     try
                    {
                        string abc= "";
                        abc= ConfigurationManager.AppSettings.Get("abc");
                        
    
                        DateTime dattme= File.GetLastAccessTime(abc);
                        
    
                        Console.WriteLine("the last access time for  c {0}", dattme);
                       
                        if(DateTime.Now.Minute > 4)
                        {
                            DataClasses1DataContext db = new DataClasses1DataContext();
                            tbl_OutBox tb = new tbl_OutBox();
                            tb.ToSIM_No = "+234234324";
                            tb.ToText = "Check please";
                            tb.FromSIM_No = "+234234324";
                            tb.FromText = "Check please";
                            db.tbl_OutBoxes.InsertOnSubmit(tb);
                            db.SubmitChanges();
                            
                        }
    
    
                       
    
                        
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine("The process failed: {0}", e.ToString());
                    }
                    Thread.Sleep(5000);
                }
            }

    how to solve 

    Wednesday, May 11, 2016 10:41 AM

Answers

  • User-286291038 posted

    Hi Bakhtawar,

    According to your requirement, I think you may have to subtract  the current time minute with the  minute value from the GetLastAccessTime minute (the dattme variable you have created)


    ....
    if((DateTime.Now.Minute - dattme.Minute) > 4)
    ....

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, May 11, 2016 2:34 PM
  • User-1716253493 posted
            if (DateTime.Now > dattme.AddMinutes(4))
            {
            }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, May 12, 2016 12:26 AM

All replies

  • User-286291038 posted

    Hi Bakhtawar,

    According to your requirement, I think you may have to subtract  the current time minute with the  minute value from the GetLastAccessTime minute (the dattme variable you have created)


    ....
    if((DateTime.Now.Minute - dattme.Minute) > 4)
    ....

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, May 11, 2016 2:34 PM
  • User-434868552 posted

    [edited to  correct my  coding errors]

    @Bakhtawar As...

    EDIT 2016 May 17th:

    Nataraj Gand... has edited the answer above since my reply;
    however, his code is still fatally flawed.  See the edit at the
    bottom of this reply.

    END EDIT 2016 May 17th.

    The code from Nataraj Gand... is fatally flawed.

    first, dattme.Now.Minute is wrong; correct is dattme.Minute.

    Second, the expression type of (DateTime.Now.Minute > dattme.Minute) is System.Boolean.

    Since  (DateTime.Now.Minute > dattme.Minute) is System.Boolean,
            ((System.Boolean) > 4) creates a compiler error:
        CS0019 Operator '>' cannot be applied to operands of type 'bool' and 'int'

    // if((DateTime.Now.Minute > dattme.Now.Minute) > 4)
    
    DateTime dattme = new DateTime(1947, 4, 28, 10, 58, 30);
    DateTime fakeDateTimeNow = new DateTime(1947, 4, 28, 11, 3, 30);
    Console.WriteLine(fakeDateTimeNow);
    Console.WriteLine(dattme);
    var nowresult = fakeDateTimeNow.Minute > dattme.Minute;
    Console.WriteLine(nowresult);
    Console.WriteLine(nowresult.GetType());

    output:

    1947-04-28 00:11:03
    1947-04-28 00:10:58
    False
    
    typeof(Boolean)
    System.Boolean

    While the idea from Nataraj Gand... works some of the time, it fails when DateTime.Now.Minute has certain values, for example:

    10:58   dattme.Now.Minute
    11:03   DateTime.Now.Minute

    03 is less than 58

    You need either to use TimeSpan or to use the simpler easy to read code from oned_gk below.

    https://msdn.microsoft.com/en-us/library/system.timespan(v=vs.110).aspx

    Note:  use TotalMinutes:

    TimeSpan elapsedA = fakeDateTimeNow - dattme; // positive value
    TimeSpan elapsedB = dattme - fakeDateTimeNow; // negative value
    Console.WriteLine("A:");
    Console.WriteLine(elapsedA);
    Console.WriteLine("B:");
    Console.WriteLine(elapsedB);
    Console.WriteLine(elapsedA.TotalMinutes);
    Console.WriteLine(elapsedB.TotalMinutes);

    output:

    A:
    00:05:00
    B:
    -00:05:00
    5
    -5

    now we can do this:

    if((elapsedA.TotalMinutes) > 4) Console.WriteLine("reject condition");
    else Console.WriteLine("acceptable condition");

    output:

    reject condition

    here is the complete code in one chunk:

    DateTime dattme = new DateTime(1947, 4, 28, 10, 58, 30);
    DateTime fakeDateTimeNow = new DateTime(1947, 4, 28, 11, 3, 30);
    Console.WriteLine(fakeDateTimeNow);
    Console.WriteLine(dattme);
    var nowresult = fakeDateTimeNow.Minute > dattme.Minute;
    Console.WriteLine(nowresult);
    Console.WriteLine(nowresult.GetType());
    TimeSpan elapsedA = fakeDateTimeNow - dattme; // positive value
    TimeSpan elapsedB = dattme - fakeDateTimeNow; // negative value
    Console.WriteLine("A:");
    Console.WriteLine(elapsedA);
    Console.WriteLine("B:");
    Console.WriteLine(elapsedB);
    Console.WriteLine(elapsedA.TotalMinutes);
    Console.WriteLine(elapsedB.TotalMinutes);
    if ((elapsedA.TotalMinutes) > 4) Console.WriteLine("reject condition");
    else Console.WriteLine("acceptable condition");

    EDIT:

    the code by oned_gk below is better for its easy to read simplicity.  http://forums.asp.net/post/6050508.aspx 

    // simpler code by oned_gk
    if (fakeDateTimeNow > dattme.AddMinutes(4)) Console.WriteLine("reject condition");
    else Console.WriteLine("acceptable condition");

    output:

    reject condition

    END EDIT.

    END 2016 May 17th:

     This edit demonstrates the flaw in the code by Nataraj Gand... at http://forums.asp.net/post/6050421.aspx 

    DateTime fiveMinutesAgo = DateTime.Now.AddMinutes(-5);
    DateTime fakeNowTime    = fiveMinutesAgo.AddMinutes(5);
    // model compare is based on
    //   if((DateTime.Now.Minute - dattme.Minute) > 4)
    // since fakeNowTime is 5 minutes after fiveMinutesAgo,
    // the comparison SHOULD ALWAYS be TRUE.
    if((fakeNowTime.Minute - fiveMinutesAgo.Minute) > 4)
       Console.WriteLine("Current Time {0} is more than 4 minutes after 5 minutes ago {1}",
                         fakeNowTime, fiveMinutesAgo);
    else
        Console.WriteLine("!!!!! Current Time {0} is NOT MORE than 4 minutes after 5 minutes ago {1}",
                          fakeNowTime, fiveMinutesAgo);
    Console.WriteLine("###########################################################################");
    // =================================
    // loop through the next 60 minutes:
    DateTime nextFiveMinutesAgo;
    DateTime nextFakeTimeNow;
    for (Int32 minutesToAdd = 1; minutesToAdd < 61; minutesToAdd++)
    {
        nextFiveMinutesAgo = fiveMinutesAgo.AddMinutes(minutesToAdd);
        nextFakeTimeNow    = fakeNowTime.AddMinutes(minutesToAdd);
        if ((nextFakeTimeNow.Minute - nextFiveMinutesAgo.Minute) > 4)
            Console.WriteLine("Current Time {0} is more than 4 minutes after 5 minutes ago {1}",
                              nextFakeTimeNow, nextFiveMinutesAgo);
        else
            Console.WriteLine("!!!!! Current Time {0} is NOT MORE than 4 minutes after 5 minutes ago {1}",
                              nextFakeTimeNow, nextFiveMinutesAgo);
    }

    Run the above code in LINQPad https://www.linqpad.net/ or Visual Studio, example output:

    Current Time 2016-05-17 15:40:30 is more than 4 minutes after 5 minutes ago 2016-05-17 15:35:30
    ###########################################################################
    Current Time 2016-05-17 15:41:30 is more than 4 minutes after 5 minutes ago 2016-05-17 15:36:30
    Current Time 2016-05-17 15:42:30 is more than 4 minutes after 5 minutes ago 2016-05-17 15:37:30
                                .                                                              .

                                .                                                              .

                                .                                                              .

    Current Time 2016-05-17 15:58:30 is more than 4 minutes after 5 minutes ago 2016-05-17 15:53:30
    Current Time 2016-05-17 15:59:30 is more than 4 minutes after 5 minutes ago 2016-05-17 15:54:30
    !!!!! Current Time 2016-05-17 16:00:30 is NOT MORE than 4 minutes after 5 minutes ago 2016-05-17 15:55:30
    !!!!! Current Time 2016-05-17 16:01:30 is NOT MORE than 4 minutes after 5 minutes ago 2016-05-17 15:56:30
    !!!!! Current Time 2016-05-17 16:02:30 is NOT MORE than 4 minutes after 5 minutes ago 2016-05-17 15:57:30
    !!!!! Current Time 2016-05-17 16:03:30 is NOT MORE than 4 minutes after 5 minutes ago 2016-05-17 15:58:30
    !!!!! Current Time 2016-05-17 16:04:30 is NOT MORE than 4 minutes after 5 minutes ago 2016-05-17 15:59:30
    Current Time 2016-05-17 16:05:30 is more than 4 minutes after 5 minutes ago 2016-05-17 16:00:30
    Current Time 2016-05-17 16:06:30 is more than 4 minutes after 5 minutes ago 2016-05-17 16:01:30
                                .                                                              .

                                .                                                              .

                                .                                                              .

    Current Time 2016-05-17 16:39:30 is more than 4 minutes after 5 minutes ago 2016-05-17 16:34:30
    Current Time 2016-05-17 16:40:30 is more than 4 minutes after 5 minutes ago 2016-05-17 16:35:30

    END EDIT 2016 May 17th.

     

    Wednesday, May 11, 2016 3:56 PM
  • User-1716253493 posted
            if (DateTime.Now > dattme.AddMinutes(4))
            {
            }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, May 12, 2016 12:26 AM