2013-09-01 153 views
-1

我正在嘗試將手機當前時間添加到我的日期時間列表中。我需要它能夠減去蜱。我嘗試過使用phonecurrentime.ToString("dd hh:mm");,但是因爲它是一個字符串,所以沒有滴答和各種錯誤!我需要它DateTime.now如何使用DateTime.Now打勾

這裏是我的代碼:

InitializeComponent(); 

List<DateTime> theDates = new List<DateTime>(); 
DateTime fileDate, closestDate; 

theDates.Add(new DateTime(2000, 1, 1, 10, 29, 0)); 
theDates.Add(new DateTime(2000, 1, 1, 3, 29, 0)); 
theDates.Add(new DateTime(2000, 1, 1, 3, 29, 0)); 

// This is the date that should be found 
theDates.Add(new DateTime(2000, 1, 1, 4, 22, 0)); 

// This is the date you want to find the closest one to 
fileDate = DateTime.Now; 

long min = long.MaxValue; 

foreach (DateTime date in theDates) 
{ 
    if (Math.Abs(date.Ticks - fileDate.Ticks) < min) 
    { 
     min = Math.Abs(date.Ticks - fileDate.Ticks); 
     closestDate = date; 
    } 
} 
+0

什麼類型是phonecurrentime?它甚至編譯? –

+4

看起來像[XY-Problem](http://www.perlmonks.org/?node_id=542341) – I4V

+0

請澄清你的問題。問題出在哪裏,哪裏遇到問題?以及爲什麼你使用'phonecurrentime.ToString(「dd hh:mm」)'爲什麼你不使用'DateTime.Now'? – ehsan88

回答

0
fileDate = phonecurrentime.ToString("dd hh:mm"); 

不會編譯。 fileDateDateTime對象。您需要將其分配給另一個DateTime對象,而不是string

如果phonecurrenttimeDateTime,則可以省略.ToString()方法。

fileDate = phonecurrenttime; 

編輯

從你的評論,如果你只是想在當前ddate /時間分配給fileDate你可以使用DateTime.Now

fileDate = DateTime.Now; 
+0

它不會與datetime.now雖然 – Jeremy

+0

@Jeremyc - 它會。你需要做fileDate = DateTime.Now; –

+0

最近的時間沒有得到更大的數字。如果最接近的時間是11:30,那麼列表中最大的時間就是17:00,例如它應該說11:30 - Jeremyc剛剛編輯 – Jeremy

2

,如果你有一個字符串,並希望將其轉換爲DateTime即可使用

CultureInfo cf = new CultureInfo("en-us"); 

if(DateTime.TryParseExact("12 12:45", "dd hh:mm", cf, DateTimeStyles.None, out fileDate)) 
{ 
    // your code 
} 

和你的代碼看起來像:

List<DateTime> theDates = new List<DateTime>(); 
    DateTime fileDate, closestDate; 

    theDates.Add(new DateTime(2000, 1, 1, 10, 29, 0)); 
    theDates.Add(new DateTime(2000, 1, 1, 3, 29, 0)); 
    theDates.Add(new DateTime(2000, 1, 1, 3, 29, 0)); 

    // This is the date that should be found 
    theDates.Add(new DateTime(2000, 1, 1, 4, 22, 0)); 

    CultureInfo cf = new CultureInfo("en-us"); 
    string timeToParse = phonecurrentime.ToString("dd hh:mm"); 

    if(DateTime.TryParseExact(timeToParse, "dd hh:mm", cf, DateTimeStyles.None, out fileDate)) 
    { 
     long min = long.MaxValue; 

     foreach (DateTime date in theDates) 
     { 
      if (Math.Abs(date.Ticks - fileDate.Ticks) < min) 
      { 
       min = Math.Abs(date.Ticks - fileDate.Ticks); 
       closestDate = date; 
      } 
     } 
    } 

,如果你想的日期時間的部分時間比較您可以使用TimeOfDay屬性:上述

 TimeSpan ts = DateTime.Now.TimeOfDay; 

     foreach (DateTime date in theDates) 
     { 
      long diff = Math.Abs(ts.Ticks - date.TimeOfDay.Ticks); 

      if (diff < min) 
      { 
       min = diff; 
       closestDate = date; 
      } 
     } 
+0

我實際上並不需要它到一個字符串。我只需要datetime.now來處理ticks。將嘗試 – Jeremy

+0

@Jeremyc如果你看看我的答案的結尾,你會發現我使用DateTime.Now在ticks中,並且只使用DateTime的時間部分 –

1

達倫·戴維斯是正確的。

您可以添加/減去日期時間對象。結果爲TimeSpan,可讓您輕鬆比較日期和/或時間差異。

此外,您應該爲每個添加到列表中的日期命名(分配給變量,然後添加到列表中)。一個月後,你不會記得每一天的意思;)

+0

雖然它不適用於datetime.now – Jeremy