计算两个时间的前后 时间戳

本文介绍了一个简单的C#应用程序示例,该程序实现了将DateTime转换为Unix时间戳的功能,并通过比较当前时间和用户选择的时间来判断是否可以进行定时邮件发送。文章通过具体的代码片段展示了如何使用C#进行时间戳的转换以及如何实现基本的时间逻辑判断。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

        private void buttonSure_Click(object sender, EventArgs e)
        {
           // MessageBox.Show(this.userControlChoiceTime1.GetUserChoiceTime().ToLocalTime().ToString());
            DateTime userChoiceTime = this.userControlChoiceTime1.GetUserChoiceTime();
            //TimeSpan ts1 = new TimeSpan(userChoiceTime.Ticks);
            //TimeSpan ts2 = new TimeSpan(DateTime.Now.Ticks);
            //TimeSpan ts = ts1.Subtract(ts2).Duration();
            if (ConvertDateTimeInt(userChoiceTime) - ConvertDateTimeInt(DateTime.Now)<0)
            {
                MessageBox.Show("O(∩_∩)O貌似不能回到过去发邮件!");
            }
        }

        // DateTime时间格式转换为Unix时间戳格式              
        public int ConvertDateTimeInt(System.DateTime time)              
        {                  
            System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));                  
            return (int)(time - startTime).TotalSeconds;              
        } 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值