C# 自特定日期以来的秒数

发布于 2024-08-29 08:33:45 字数 44 浏览 2 评论 0 原文

在 C# 3.0 中,如何获取自 2010 年 1 月 1 日以来的秒数?

In C# 3.0, how do I get the seconds since 1/1/2010?

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

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

发布评论

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

评论(6

殤城〤 2024-09-05 08:33:45

是这样的:

  TimeSpan test = DateTime.Now - new DateTime(2010, 01, 01);
  MessageBox.Show(test.TotalSeconds.ToString());

为了一个班轮的乐趣:

 MessageBox.Show((DateTime.Now - new DateTime(2010, 01, 01))
     .TotalSeconds.ToString());

Goes like this:

  TimeSpan test = DateTime.Now - new DateTime(2010, 01, 01);
  MessageBox.Show(test.TotalSeconds.ToString());

For one liner fun:

 MessageBox.Show((DateTime.Now - new DateTime(2010, 01, 01))
     .TotalSeconds.ToString());
心是晴朗的。 2024-09-05 08:33:45

您可以减去 2 个 DateTime 实例并得到一个 TimeSpan:

DateTime date = new DateTime(2010,1,1);
TimeSpan diff = DateTime.Now - date;
double seconds = diff.TotalSeconds;

You can substract 2 DateTime instances and get a TimeSpan:

DateTime date = new DateTime(2010,1,1);
TimeSpan diff = DateTime.Now - date;
double seconds = diff.TotalSeconds;
_蜘蛛 2024-09-05 08:33:45

只是为了避免时区问题

 TimeSpan t = (DateTime.UtcNow - new DateTime(2010, 1, 1));

 int timestamp  = (int) t.TotalSeconds;

 Console.WriteLine (timestamp);

Just to avoid timezone issues

 TimeSpan t = (DateTime.UtcNow - new DateTime(2010, 1, 1));

 int timestamp  = (int) t.TotalSeconds;

 Console.WriteLine (timestamp);
对风讲故事 2024-09-05 08:33:45

这实际上取决于您正在使用谁的 2010-Jan-01 以及您是否希望考虑夏令时。

//I'm currently in Central Daylight Time (Houston, Texas)
DateTime jan1 = new DateTime(2010, 1, 1);

//days since Jan1 + time since midnight
TimeSpan differenceWithDaylightSavings = DateTime.Now - jan1;

//one hour less than above (we "skipped" those 60 minutes about a month ago)
TimeSpan differenceWithoutDaylightSavings = (DateTime.UtcNow - jan1.ToUniversalTime());

//difference for those using UTC and 2010-Jan-01 12:00:00 AM UTC as their starting point
//   (today it's 5 hours longer than differenceWithDaylightSavings)
TimeSpan utcDifference = (DateTime.UtcNow - new DateTime(2010, 1, 1));
Difference with Daylight Savings:     105.15:44:09.7003571
Difference without Daylight Savings:  105.14:44:09.7003571
UTC Difference:                       105.20:44:09.7003571

要获取秒数,请使用 TotalSeconds 属性关闭TimeSpan 对象。

It's really a matter of whose 2010-Jan-01 you're using and whether or not you wish to account for daylight savings.

//I'm currently in Central Daylight Time (Houston, Texas)
DateTime jan1 = new DateTime(2010, 1, 1);

//days since Jan1 + time since midnight
TimeSpan differenceWithDaylightSavings = DateTime.Now - jan1;

//one hour less than above (we "skipped" those 60 minutes about a month ago)
TimeSpan differenceWithoutDaylightSavings = (DateTime.UtcNow - jan1.ToUniversalTime());

//difference for those using UTC and 2010-Jan-01 12:00:00 AM UTC as their starting point
//   (today it's 5 hours longer than differenceWithDaylightSavings)
TimeSpan utcDifference = (DateTime.UtcNow - new DateTime(2010, 1, 1));
Difference with Daylight Savings:     105.15:44:09.7003571
Difference without Daylight Savings:  105.14:44:09.7003571
UTC Difference:                       105.20:44:09.7003571

To get the seconds, use the TotalSeconds property off the TimeSpan object.

奢望 2024-09-05 08:33:45
protected void Page_Load(object sender, EventArgs e)
{
    SecondsSinceNow(new DateTime(2010, 1, 1, 0, 0, 0));
}

private double SecondsSinceNow(DateTime compareDate)
{
    System.TimeSpan timeDifference = DateTime.Now.Subtract(compareDate);
    return timeDifference.TotalSeconds;
}
protected void Page_Load(object sender, EventArgs e)
{
    SecondsSinceNow(new DateTime(2010, 1, 1, 0, 0, 0));
}

private double SecondsSinceNow(DateTime compareDate)
{
    System.TimeSpan timeDifference = DateTime.Now.Subtract(compareDate);
    return timeDifference.TotalSeconds;
}
别把无礼当个性 2024-09-05 08:33:45
DateTime t1 = DateTime.Now;
DateTime p = new DateTime(2010, 1, 1);

TimeSpan d = t1 - p;

long s = (long)d.TotalSeconds;
MessageBox.Show(s.ToString());
DateTime t1 = DateTime.Now;
DateTime p = new DateTime(2010, 1, 1);

TimeSpan d = t1 - p;

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