Datetime.now as TimeSpan value?

前端 未结 7 1385
小蘑菇
小蘑菇 2020-12-17 09:00

I need the current Datetime minus myDate1 in seconds.

DateTime myDate1 = new DateTime(1970, 1, 9, 0, 0, 00);
DateTime myDate2 = DateTime.Now;

T         


        
相关标签:
7条回答
  • 2020-12-17 09:33

    you need to get .TotalSeconds property of your timespan :

    DateTime myDate1 = new DateTime(2012, 8, 13, 0, 05, 00);
    DateTime myDate2 = DateTime.Now;
    TimeSpan myDateResult = new TimeSpan();
    myDateResult = myDate2 - myDate1;
    MessageBox.Show(myDateResult.TotalSeconds.ToString());
    
    0 讨论(0)
  • 2020-12-17 09:37
    TimeSpan myDateResult;
    
    myDateResult = DateTime.Now.Subtract(new DateTime(1970,1,9,0,0,00));
    myDateResult.TotalSeconds.ToString();
    
    0 讨论(0)
  • 2020-12-17 09:41

    Your code is correct. You have the time difference as a TimeSpan value, so you only need to use the TotalSeconds property to get it as seconds:

    DateTime myDate1 = new DateTime(1970, 1, 9, 0, 0, 00);
    DateTime myDate2 = DateTime.Now;
    
    TimeSpan myDateResult;
    
    myDateResult = myDate2 - myDate1;
    
    double seconds = myDateResult.TotalSeconds;
    
    0 讨论(0)
  • 2020-12-17 09:42

    Have you tried something like

    DateTime.Now.Subtract(new DateTime(1970, 1, 9, 0, 0, 00)).TotalSeconds
    

    DateTime.Subtract Method (DateTime)

    TimeSpan.TotalSeconds Property

    0 讨论(0)
  • 2020-12-17 09:43

    How about

    myDateResult.TotalSeconds
    

    http://msdn.microsoft.com/en-us/library/system.timespan.totalseconds

    0 讨论(0)
  • 2020-12-17 09:44

    Code:

    TimeSpan myDateResult = DateTime.Now.TimeOfDay;
    
    0 讨论(0)
提交回复
热议问题