2017-01-04 5 views
0

DATETIME値1980datetimeの値が有効(完全な日付)か、それとも年だけであるかを判断する方法はありますか?

私は、SQL

string startDate = ed.IDAFrom != null ? Convert.ToDateTime(ed.IDAFrom).ToShortDateString() : ""; 

EDIT

から日時を取得するには、このコードを持っている注:年(フル日付でなければなりませんここに日付を卒業されました)

私は、私の見解で日付ピッカーを持っている(ので、日付が府にありますあなたが卒業したときの日付を忘れたら、ユーザーは日付ピッカーを年だけ編集するでしょうか?ユーザーエンコード完全な日付や年は、私は、ユーザーが完全な日付をエンコードする場合YEARの値を取得したい場合、私はここに達成したい何

です。

希望しています。

ありがとうございました。

コード

foreach (var ed in exams) 
     { 
      int rowId = i; 
      string startDate = ed.IDAFrom != null ?Convert.ToDateTime(ed.IDAFrom).ToShortDateString() : ""; 
      string endDate = ed.IDATo != null ? Convert.ToDateTime(ed.IDATo).ToShortDateString() : ""; 
      string InclusiveDates = startDate + " - " + endDate; 
      rowsObj[i] = new { id = rowId, cell = new object[] { rowId, InclusiveDates } }; 
      i++; 
     } 
+0

です'?それは文字列としてデータベースに格納されていますか? – krillgar

+0

'bool isYearOnly =(ed.IDAFrom!= null)&&(ed.IDAFrom.Trim()。Length == 4);' – Blorgbeard

+0

IDAFromとは何ですか? – CodingYoshi

答えて

2

私はそれが完全な日付である場合にのみ、年を表示します。

DateTime value; 

if(DateTime.TryParse(ed.IDAFrom, out value)) 
{ 
    Int year = value.Year; 
} 

あなたが文化を気にしている場合:

CultureInfo = CultureInfo.CreateSpecificCulture("en-US"); // Or whichever culture you need 
if (DateTime.TryParse(ed.IDAFrom, culture, DateTimeStyles.None, out value)) 
{ 
    int year = value.Year; 
} 

ユーザーは、年のみまたは完全な日付を入力して、このメソッドを使用するには、いずれかのオプションを持っている場合:

public static string GetDateTime(string value) 
{ 
    DateTime date; 
    string dateString = ""; // Empty by default 

    // If full date is given, this will succeed 
    if (DateTime.TryParse(value, out date)) 
    { 
     dateString = date.ToShortDateString(); 
    } 
    // If only year is given then this will succeed 
    else if (DateTime.TryParseExact(value, 
      "yyyy", 
      CultureInfo.InvariantCulture, 
      DateTimeStyles.None, 
      out date)) 
    { 
     dateString = date.ToShortDateString(); 
    } 

    return dateString; 
} 

編集

今、あなたはあなたの質問にいくつかのより多くのコードが追加されていることを、ここでのLINQを使用してそれを行う方法です:

int i = 0; 
int j = 0; 
var rows = list.Select(exam => 
{ 

    string inclusiveDates = string.Format("{0} - {1}", GetDateTime(exam.IDAFrom), GetDateTime(exam.IDATo)); 
    return new 
    { 
     Id = ++i, 
     Cell = new object[] { ++j, inclusiveDates } 
    }; 
}) 
.ToList(); 

そして、ここでは `ed.IDAFromでどのようなデータ型の使用例

class Program 
{ 
    public class Exam 
    { 
     public string IDAFrom { get; set; } 
     public string IDATo { get; set; } 
    } 

    public static string GetDateTime(string value) 
    { 
     DateTime date; 
     string dateString = ""; // Empty by default 

     // If full date is given, this will succeed 
     if (DateTime.TryParse(value, out date)) 
     { 
      dateString = date.ToShortDateString(); 
     } 
     // If only year is given then this will succeed 
     else if (DateTime.TryParseExact(value, 
       "yyyy", 
       CultureInfo.InvariantCulture, 
       DateTimeStyles.None, 
       out date)) 
     { 
      dateString = date.ToShortDateString(); 
     } 

     return dateString; 
    } 

    static void Main(string[] args) 
    { 

     var list = new List<Exam> { new Exam { IDAFrom = "1999", IDATo = null }, 
     new Exam { IDAFrom = DateTime.Now.ToShortDateString(), IDATo = DateTime.Now.AddDays(5).ToShortDateString() } }; 
     int i = 0; 
     int j = 0; 
     var rows = list.Select(exam => 
     { 

      string inclusiveDates = string.Format("{0} - {1}", GetDateTime(exam.IDAFrom), GetDateTime(exam.IDATo)); 
      return new 
      { 
       Id = ++i, 
       Cell = new object[] { ++j, inclusiveDates } 
      }; 
     }) 
     .ToList(); 


     foreach (var item in rows) 
     { 
      Console.WriteLine("{0}\t{1}\t{2}", item.Id.ToString(), item.Cell[0], item.Cell[1]); 
     } 
     Console.Read(); 

    } 
} 
+0

私の質問にいくつかのコードを追加しました。 – KiRa

+0

私はそれを見ます、なぜこの答えはあなたの質問に答えませんか? – CodingYoshi

+0

申し訳ありません。私はコードをテストし、上記の追加されたコードにそれを適用します。 – KiRa

関連する問題