如何将当前时间设置为一个值

发布于 2024-12-19 15:35:15 字数 165 浏览 0 评论 0原文

我只是想知道是否有办法获取当前时间并将其设置为一个值。

如果是 12:06 AM.. 我想获取该时间并将其设置为 currentTime。

例子

float currentTime = 0;
currentTime = 12.06;

I was just wondering if there is a way to get the current time and set it into a value.

If its 12:06 AM.. I want to get that time and set it into currentTime.

Example

float currentTime = 0;
currentTime = 12.06;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

夏了南城 2024-12-26 15:35:15

正如其他人提到的,DateTime 类非常适合此目的,并计算出 2 个日期/时间之间的差异:

DateTime end = DateTime.Now;
DateTime start = new DateTime(2011, 12, 5, 12, 6,0);

double hours = (end - start).TotalHours;

DateTime 对象的减法会产生一个 TimeSpan 对象,您可以使用它来查看小时/分钟等。

As others have mentioned, the DateTime class would be ideal for this, and to work out the difference between 2 date/times:

DateTime end = DateTime.Now;
DateTime start = new DateTime(2011, 12, 5, 12, 6,0);

double hours = (end - start).TotalHours;

The subtraction of DateTime objects results in a TimeSpan object that you can use to see the hours/minutes etc.

荆棘i 2024-12-26 15:35:15

尝试 DateTime 类

  DateTime dt = DateTime.Now;

try DateTime class

  DateTime dt = DateTime.Now;
硪扪都還晓 2024-12-26 15:35:15

这是您要找的吗?

DateTime currentTime;

currentTime = DateTime.Now;

Is this what you're looking for?

DateTime currentTime;

currentTime = DateTime.Now;
不必在意 2024-12-26 15:35:15

不要使用浮点数或字符串。您可以使用 DateTime 做各种很酷的事情。

您可以通过以下方式获取某人的工作时间:

var clockIn = new DateTime(2011,12,4,9,0,0);   // December 4th, 9 AM
var clockOut = new DateTime(2011,12,4,17,0,0); // December 4th, 5 PM

var duration = clockOut - clockIn;             // TimeSpan

Console.Write(duration.TotalHours);            // 8

Don't use floats or strings. You can do all kinds of cool things using DateTime.

Here's how you'd get the hours that someone worked:

var clockIn = new DateTime(2011,12,4,9,0,0);   // December 4th, 9 AM
var clockOut = new DateTime(2011,12,4,17,0,0); // December 4th, 5 PM

var duration = clockOut - clockIn;             // TimeSpan

Console.Write(duration.TotalHours);            // 8
左岸枫 2024-12-26 15:35:15

有几个人提到了如何操作,但作为“更好”的建议,您应该使用

DateTime currentTime = DateTime.UtcNow

否则,如果您的计时代码在那些日子运行,那么当时钟倒转时您会遇到问题。 (另外,将 UTC 时间更改为本地时间比将“凌晨 1 点”转换为 UTC 容易得多(因为当时钟倒转时将出现两个)

A few people have mentioned how, but as a 'better' recommendation you should use

DateTime currentTime = DateTime.UtcNow

Otherwise you have issues when the clocks go back, if your timing code is run on those days. (plus it is far easier to alter the UTC time to local time than it is to convert a '1am' to UTC (as there will be two of them when the clocks go back)

许你一世情深 2024-12-26 15:35:15

好吧,如果您真的将其视为浮点数,请尝试:

var currentDate = DateTime.Now; 
float currentTime = float.Parse((currentDate.Hour > 12 ? currentDate.Hour -12 :
currentDate.Hour)  + "." + currentDate.Minute);

我不建议将日期或时间与浮点数进行比较。更好的选择是使用时间跨度。

Well if you really what it as a float then try:

var currentDate = DateTime.Now; 
float currentTime = float.Parse((currentDate.Hour > 12 ? currentDate.Hour -12 :
currentDate.Hour)  + "." + currentDate.Minute);

I wouldn't recommend comparing dates or time with floats. A better options would be to use timespans.

淑女气质 2024-12-26 15:35:15

您应该使用 Timespan 实例来获取与时间相关的值,您可以使用灵活性来获取所需的值,例如

TimeSpan ts = DateTime.Now.TimeOfDay;
ts.ToString("hh:mm") // this could be what you are looking for

您可以使用 ts.TotalHours 这将为您提供小数小时(作为 double),否则你可以专门使用 ts.Hours ..ts.Minutes 来构造一个字符串,它可能会被证明是有用的。

You should be using a Timespan instance for time related values, you can use the flexibility to get the required values like

TimeSpan ts = DateTime.Now.TimeOfDay;
ts.ToString("hh:mm") // this could be what you are looking for

You could then use ts.TotalHours which would give you fractional hours (as a double) else you could construct a string specifically using ts.Hours ..ts.Minutes play around and it could be prove useful.

迷荒 2024-12-26 15:35:15

请尝试以下操作:

 DateTime StartTime=StartTime value;
 DateTime CurrentTime=DateTime.Now;
 TimeSpan dt = CurrentTime.Subtract(StartTime);

dt 中,您将获得一个工作时间段。

Try the following:

 DateTime StartTime=StartTime value;
 DateTime CurrentTime=DateTime.Now;
 TimeSpan dt = CurrentTime.Subtract(StartTime);

In dt you will get a working time period.

白云悠悠 2024-12-26 15:35:15

如果你想得到两个时间之间的差异,那么可以这样做:

DateTime dateOne = DateTime.Parse(enteredTime);
DateTime dateTwo = DateTime.Now;

TimeSpan difference = dateOne - dateTwo;

If you want to have the difference between two times, then do this:

DateTime dateOne = DateTime.Parse(enteredTime);
DateTime dateTwo = DateTime.Now;

TimeSpan difference = dateOne - dateTwo;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文