获取自此以来的秒数

发布于 2024-10-15 22:50:00 字数 195 浏览 6 评论 0原文

我正在用 C# 编写一个应用程序,我很想知道如何获取每个月的秒数。例如,今天,2 月 3 日,我很想知道:

January: 2678400
February: 264000

基本上,我很想知道过去几个月有多少秒以及当前月份有多少秒(到目前为止有多少秒)。

任何代码片段将不胜感激......

I am writing an application in C#, and I would love to know how you can get the number of seconds in each month. For example, today, February the 3th, I would love to have:

January: 2678400
February: 264000

Basically, I would love to know how many seconds in the past months and how many seconds in the current month at the current time (how many seconds so far).

Any code snippets would be appreciated....

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

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

发布评论

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

评论(3

柳絮泡泡 2024-10-22 22:50:00

从一个日期减去另一个日期总会得到差值的 TimeSpan

TimeSpan diff = (new DateTime(2011, 02, 10) - new DateTime(2011, 02, 01));

Console.WriteLine(diff.TotalSeconds);

Subtracting one date from another will always give you a TimeSpan of the difference:

TimeSpan diff = (new DateTime(2011, 02, 10) - new DateTime(2011, 02, 01));

Console.WriteLine(diff.TotalSeconds);
往昔成烟 2024-10-22 22:50:00

您可以将两个日期相减并得到它们之间的总秒数。

DateTime start = new DateTime(2011, 02, 03);
DateTime end = DateTime.Now;
var seconds = (start - end).TotalSeconds; 

两个日期相减的结果是 TimeSpan

You can subtract two dates from each other and get the total seconds between them.

DateTime start = new DateTime(2011, 02, 03);
DateTime end = DateTime.Now;
var seconds = (start - end).TotalSeconds; 

The result of subtracting two dates from each other is a TimeSpan.

最偏执的依靠 2024-10-22 22:50:00

您可以从 TimeSpan 获取 TotalSeconds 属性:

TimeSpan ts = DateTime.Now.Subtract(new DateTime(2011,2,1));
Console.Write(ts.TotalSeconds);

将为您提供本月到目前为止的秒数。

You can get the TotalSeconds property from a TimeSpan:

TimeSpan ts = DateTime.Now.Subtract(new DateTime(2011,2,1));
Console.Write(ts.TotalSeconds);

will give you the seconds so far this month.

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