如何在C#窗体中将int转换为datetime [英] How to convert int to datetime in C# windows form

查看:691
本文介绍了如何在C#窗体中将int转换为datetime的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

美好的一天每个人都请我想将int转换为日期时间但我收到错误



Good day everyone please i want to convert int to date time but i am getting an error

Additional information: Invalid cast from 'Int32' to 'DateTime'.





我尝试了什么:





What I have tried:

TimeSpan ts = retdt - reqdt;

           int days = ts.Days;

       DateTime cde = Convert.ToDateTime(days);


           if (cde <=before)
           {
               result = Convert.ToDouble(txtamount.Text);
               double mult = result * 5d;
               txtresult.Text = mult.ToString();

推荐答案

你将不得不找出int值何时开始。整数日期/时间值从某个日期和时间开始,并计算一个秒,毫秒,刻度,...的数字,以表示相对于开始的日期/时间。



例如,C通常使用1970年1月1日00:00:00作为开始时间,而表示日期/时间的32位整数是从那时起的秒数。那个时钟在2038年1月19日凌晨3点结束。



那么,你所代表的整数与开始日期/时间相关的是什么?



一旦你搞清楚了,只需创建一个代表整数开始日期/时间的日期/时间实例并添加,就可以很容易地将其转换为.NET日期/时间。整数表示的秒数或整数。
You're going to have to find out when the int value starts. Integer date/time values start from a certain date and time and count up a number of either seconds, milliseconds, ticks, ... to represent a date/time relative to the start.

For example, a C commonly uses 00:00:00 Jan 1st, 1970 for the starting time and the 32-bit integer that represents a date/time is the number of seconds since then. That clock runs out just after 3AM on January 19th, 2038.

So, what does that integer you have represent and relative to what start date/time?

Once you figure that out, it's easy to convert that to a .NET Date/Time, just by creating a date/time instance representing your integers start date/time and adding the number of seconds or whatever that integer represents to it.


Quote:

如何将int转换为datetime在C#windows窗体中

How to convert int to datetime in C# windows form



几乎与上一个问题的答案相同。

数字不是日期。 如果该数字是天数,那么您将错过起点,即初始日期。


这篇关于如何在C#窗体中将int转换为datetime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆