c# 日期转化判断实用类

方法一:Convert.ToDateTime(string)

string格式有要求,必须是yyyy-MM-dd hh:mm:ss

方法二:Convert.ToDateTime(string, IFormatProvider)

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);
        }
    }
}

方法三:DateTime.ParseExact()

string dateString = "20110526";
DateTime dt = DateTime.ParseExact(dateString, "yyyyMMdd", System.Globalization.CultureInfo.CurrentCulture);
//或者
dt = DateTime.ParseExact(dateString, "yyyyMMdd", System.Globalization.CultureInfo.InvariantCulture);
 

获取某段日期范围内的所有日期

    #region 获取某段日期范围内的所有日期
    /// <summary> 
    /// 获取某段日期范围内的所有日期,以数组形式返回  
    /// </summary>  
    /// <param name="dt1">开始日期</param>  
    /// <param name="dt2">结束日期</param>  
    /// <returns></returns>  
    private DateTime[] GetAllDays(DateTime dt1, DateTime dt2)
    {
        List<DateTime> listDays = new List<DateTime>();
        DateTime dtDay = new DateTime();
        for (dtDay = dt1; dtDay.CompareTo(dt2) <= 0; dtDay = dtDay.AddDays(1))
        {
            listDays.Add(dtDay);
        }
        return listDays.ToArray();
    }
    #endregion

判断某个日期是否在某段日期范围内

    #region 判断某个日期是否在某段日期范围内
    /// <summary> 
    /// 判断某个日期是否在某段日期范围内,返回布尔值
    /// </summary> 
    /// <param name="dt">要判断的日期</param> 
    /// <param name="dt1">开始日期</param> 
    /// <param name="dt2">结束日期</param> 
    /// <returns></returns>  
    private bool IsInDate(DateTime dt, DateTime dt1, DateTime dt2)
    {
        return dt.CompareTo(dt1) >= 0 && dt.CompareTo(dt2) <= 0;
    }
    #endregion

   #region 获取某段日期范围内的所有日期
    /// <summary>
    /// 获取某段日期范围内的所有日期,以字符串形式返回 
    /// </summary>
    /// <param name="startDate">开始日期</param>
    /// <param name="endDate">结束日期</param>
    /// <returns></returns>
    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

Java时间戳转c#字符串

    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");
    }

正则验证

/// <summary>  
/// 使用正则表达式判断是否为日期  
/// </summary>  
/// <param name="str" type=string></param>  
/// <returns name="isDateTime" type=bool></returns>  
public bool IsDateTime(string str)  
{  
    bool isDateTime = false;  
    // yyyy/MM/dd  
    if (Regex.IsMatch(str, "^(?<year>\\d{2,4})/(?<month>\\d{1,2})/(?<day>\\d{1,2})$"))  
        isDateTime = true;  
    // yyyy-MM-dd   
    else if (Regex.IsMatch(str, "^(?<year>\\d{2,4})-(?<month>\\d{1,2})-(?<day>\\d{1,2})$"))  
        isDateTime = true;  
    // yyyy.MM.dd   
    else if (Regex.IsMatch(str, "^(?<year>\\d{2,4})[.](?<month>\\d{1,2})[.](?<day>\\d{1,2})$"))  
        isDateTime = true;  
    // yyyy年MM月dd日  
    else if (Regex.IsMatch(str, "^((?<year>\\d{2,4})年)?(?<month>\\d{1,2})月((?<day>\\d{1,2})日)?$"))  
        isDateTime = true;  
    // yyyy年MM月dd日  
    else if (Regex.IsMatch(str, "^((?<year>\\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, "^(?<year>\\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, "^(?<hour>\\d{1,2})(时|点)(?<minute>\\d{1,2})分((?<second>\\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, "^(?<minute>\\d{1,2})分(?<second>\\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(?<hour>\\d{1,2})(时|点钟)\\b"))  
        isDateTime = true;  
    else  
        isDateTime = false;  
  
    return isDateTime;  
}  
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值