[英]How to determine if the value of datetime is valid(full date) or it is in year only?
DATETIME值1980
我有這段代碼從SQL檢索DateTime
string startDate = ed.IDAFrom != null ? Convert.ToDateTime(ed.IDAFrom).ToShortDateString() : "";
編輯
注意:此處的日期為畢業年份(必須為完整日期)
我的視圖中有一個日期選擇器(因此日期是完整的日期嗎?)。如果用戶畢業時忘記了日期,那么該用戶只能將日期選擇器編輯為年份。
我要在這里實現的是,如果用戶編碼了完整日期或年份,那么我想獲得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++;
}
我只想顯示完整的年份。
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();
這是一個示例用法
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();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.