C# 判断时间是否在 某一时间段内,判断时间是否是今天,获取今年第一天、最后一天,数字字符串转换为日期

1、写一个方法,设置两个时间段

protected bool getTimeSpan(string timeStr)
        {
            //判断当前时间是否在工作时间段内
            string _strWorkingDayAM = "08:30";//工作时间上午08:30
            string _strWorkingDayPM = "17:00";
            TimeSpan dspWorkingDayAM = DateTime.Parse(_strWorkingDayAM).TimeOfDay;
            TimeSpan dspWorkingDayPM = DateTime.Parse(_strWorkingDayPM).TimeOfDay;

            //string time1 = "2017-2-17 8:10:00";
            DateTime t1 = Convert.ToDateTime(timeStr);

            TimeSpan dspNow = t1.TimeOfDay;
            if (dspNow > dspWorkingDayAM && dspNow < dspWorkingDayPM)
            {
                return true;
            }
            return false;
        }

2、定义一个变量

protected string getResult = string.Empty;

3、在button单击事件中调用

 private void button4_Click(object sender, EventArgs e)
        {
            string time1 = "19:45:00";    //设置一个时间,下边调用方法返回true就是在工作时间内
            //DateTime t1 = Convert.ToDateTime(time1);
            bool resultBool = getTimeSpan(time1);
            getResult = Convert.ToString(resultBool);
            MessageBox.Show(getResult);
        }

4、效果(步骤3中设置的时间如果在步骤1设置的时间段中返回true否则相反)

 

5.判断时间是否是今天

 string start = "2020-04-23 17:00:00";
            DateTime now = DateTime.Now;
            DateTime today2 = new DateTime(now.Year, now.Month, now.Day);//当天的零时零分
            if (DateTime.Parse(start) > today2)
            {
                MessageBox.Show("yes");
            }
            else
            {
                MessageBox.Show("no");

            }

6.获取今年第一天、最后一天

/// <summary>  
        /// 获取本年年初, 本年度第一天
        /// </summary>  
        /// <param name="dateTime">当前时间 DateTime.Now or DateTime.UtcNow</param>  
        /// <returns>后面的具体时、分、秒和传入值相等</returns>  
        public static DateTime GetYearFirstDate(DateTime dateTime)
        {
            // 本年年初
            return dateTime.AddMonths(1 - dateTime.Month).AddDays(1 - dateTime.Day);
        }
 
        /// <summary>
        /// 获取本年年末, 本年度最后一天
        /// </summary>
        /// <param name="dateTime">当前时间 DateTime.Now or DateTime.UtcNow</param>  
        /// <returns>后面的具体时、分、秒和传入值相等</returns>
        public static DateTime GetYearLastDate(DateTime dateTime)
        {
            // 本年年末
            return GetYearFirstDate(dateTime).AddYears(1).AddDays(-1);
        }

7.数字字符串转换为日期类型

string dateString = "20110526";

DateTime dt = DateTime.ParseExact(dateString, "yyyyMMdd", System.Globalization.CultureInfo.CurrentCulture);

或者

DateTime dt = DateTime.ParseExact(dateString, "yyyyMMdd", System.Globalization.CultureInfo.InvariantCulture);

 

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

香煎三文鱼

你的鼓励将是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值