string格式有要求,必须是yyyy-MM-dd hh:mm:ss
using System;
using System.Globalization;
namespace NetCoreDateTime
{
class Program
{
static void Main(string[] args)
{
DateTime dt;
DateTimeFormatInfo dtFormat = new DateTimeFormatInfo();
dtFormat.ShortDatePattern = "yyyy/MM/dd";
dt = Convert.ToDateTime("2011/05/26", dtFormat);
}
}
}
string dateString = "20110526";
DateTime dt = DateTime.ParseExact(dateString, "yyyyMMdd", System.Globalization.CultureInfo.CurrentCulture);
//或者
dt = DateTime.ParseExact(dateString, "yyyyMMdd", System.Globalization.CultureInfo.InvariantCulture);
#region 获取某段日期范围内的所有日期
///
/// 获取某段日期范围内的所有日期,以数组形式返回
///
/// 开始日期
/// 结束日期
///
private DateTime[] GetAllDays(DateTime dt1, DateTime dt2)
{
List listDays = new List();
DateTime dtDay = new DateTime();
for (dtDay = dt1; dtDay.CompareTo(dt2) <= 0; dtDay = dtDay.AddDays(1))
{
listDays.Add(dtDay);
}
return listDays.ToArray();
}
#endregion
#region 判断某个日期是否在某段日期范围内
///
/// 判断某个日期是否在某段日期范围内,返回布尔值
///
/// 要判断的日期
/// 开始日期
/// 结束日期
///
private bool IsInDate(DateTime dt, DateTime dt1, DateTime dt2)
{
return dt.CompareTo(dt1) >= 0 && dt.CompareTo(dt2) <= 0;
}
#endregion
#region 获取某段日期范围内的所有日期
///
/// 获取某段日期范围内的所有日期,以字符串形式返回
///
/// 开始日期
/// 结束日期
///
protected string GetDate(DateTime startDate, DateTime endDate)
{
string result = string.Empty;
for (DateTime temp = startDate.ToShortDateString(); temp <= endDate.ToShortDateString(); temp = temp.AddDays(1))
{
if (result == string.Empty)
{
result = temp.ToString();
}
else
{
result += "," + temp.ToString();
}
}
return result;
}
#endregion
public static string GetTimeStr(long timeStamp,string trim=" ")
{
DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
long lTime = timeStamp * 10000;
TimeSpan toNow = new TimeSpan(lTime);
return dtStart.Add(toNow).ToString("yyyy-MM-dd"+trim+"HH:mm");
}
///
/// 使用正则表达式判断是否为日期
///
///
///
public bool IsDateTime(string str)
{
bool isDateTime = false;
// yyyy/MM/dd
if (Regex.IsMatch(str, "^(?\\d{2,4})/(?\\d{1,2})/(?\\d{1,2})$"))
isDateTime = true;
// yyyy-MM-dd
else if (Regex.IsMatch(str, "^(?\\d{2,4})-(?\\d{1,2})-(?\\d{1,2})$"))
isDateTime = true;
// yyyy.MM.dd
else if (Regex.IsMatch(str, "^(?\\d{2,4})[.](?\\d{1,2})[.](?\\d{1,2})$"))
isDateTime = true;
// yyyy年MM月dd日
else if (Regex.IsMatch(str, "^((?\\d{2,4})年)?(?\\d{1,2})月((?\\d{1,2})日)?$"))
isDateTime = true;
// yyyy年MM月dd日
else if (Regex.IsMatch(str, "^((?\\d{2,4})年)?(正|一|二|三|四|五|六|七|八|九|十|十一|十二)月((一|二|三|四|五|六|七|八|九|十){1,3}日)?$"))
isDateTime = true;
// yyyy年MM月dd日
else if (Regex.IsMatch(str, "^(零|〇|一|二|三|四|五|六|七|八|九|十){2,4}年((正|一|二|三|四|五|六|七|八|九|十|十一|十二)月((一|二|三|四|五|六|七|八|九|十){1,3}(日)?)?)?$"))
isDateTime = true;
// yyyy年
//else if (Regex.IsMatch(str, "^(?\\d{2,4})年$"))
// isDateTime = true;
// 农历1
else if (Regex.IsMatch(str, "^(甲|乙|丙|丁|戊|己|庚|辛|壬|癸)(子|丑|寅|卯|辰|巳|午|未|申|酉|戌|亥)年((正|一|二|三|四|五|六|七|八|九|十|十一|十二)月((一|二|三|四|五|六|七|八|九|十){1,3}(日)?)?)?$"))
isDateTime = true;
// 农历2
else if (Regex.IsMatch(str, "^((甲|乙|丙|丁|戊|己|庚|辛|壬|癸)(子|丑|寅|卯|辰|巳|午|未|申|酉|戌|亥)年)?(正|一|二|三|四|五|六|七|八|九|十|十一|十二)月初(一|二|三|四|五|六|七|八|九|十)$"))
isDateTime = true;
// XX时XX分XX秒
else if (Regex.IsMatch(str, "^(?\\d{1,2})(时|点)(?\\d{1,2})分((?\\d{1,2})秒)?$"))
isDateTime = true;
// XX时XX分XX秒
else if (Regex.IsMatch(str, "^((零|一|二|三|四|五|六|七|八|九|十){1,3})(时|点)((零|一|二|三|四|五|六|七|八|九|十){1,3})分(((零|一|二|三|四|五|六|七|八|九|十){1,3})秒)?$"))
isDateTime = true;
// XX分XX秒
else if (Regex.IsMatch(str, "^(?\\d{1,2})分(?\\d{1,2})秒$"))
isDateTime = true;
// XX分XX秒
else if (Regex.IsMatch(str, "^((零|一|二|三|四|五|六|七|八|九|十){1,3})分((零|一|二|三|四|五|六|七|八|九|十){1,3})秒$"))
isDateTime = true;
// XX时
else if (Regex.IsMatch(str, "\\b(?\\d{1,2})(时|点钟)\\b"))
isDateTime = true;
else
isDateTime = false;
return isDateTime;
}