C# 中创建时间戳的函数

发布于 2024-07-22 03:38:39 字数 177 浏览 8 评论 0原文

我想知道,有没有办法在 C# 中从日期时间创建时间戳? 我需要一个也适用于 Compact Framework 的毫秒精度值(也就是说,因为 CF 中不存在 DateTime.ToBinary() )。

我的问题是,我想以与数据库无关的方式存储这个值,这样我就可以稍后对其进行排序,并找出哪个值比另一个值更大等等。

I was wondering, is there a way to create a timestamp in c# from a datetime?
I need a millisecond precision value that also works in Compact Framework(saying that since DateTime.ToBinary() does not exist in CF).

My problem is that i want to store this value in a database agnostic way so i can sortby it later and find out which value is greater from another etc.

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

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

发布评论

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

评论(6

阳光①夏 2024-07-29 03:38:39

我总是使用如下所示的内容:

public static String GetTimestamp(this DateTime value)
{
    return value.ToString("yyyyMMddHHmmssfff");
}

这会给你一个像 200905211035131468 这样的字符串,因为该字符串从时间戳的最高位到最低位 如果你坚持的话,SQL查询中的简单字符串排序可用于按日期排序数据库中的值

I always use something like the following:

public static String GetTimestamp(this DateTime value)
{
    return value.ToString("yyyyMMddHHmmssfff");
}

This will give you a string like 200905211035131468, as the string goes from highest order bits of the timestamp to lowest order simple string sorting in your SQL queries can be used to order by date if you're sticking values in a database

眼波传意 2024-07-29 03:38:39

我相信您可以使用以下命令创建精确到秒的unix风格日期戳

//Find unix timestamp (seconds since 01/01/1970)
long ticks = DateTime.UtcNow.Ticks - DateTime.Parse("01/01/1970 00:00:00").Ticks;
ticks /= 10000000; //Convert windows ticks to seconds
timestamp = ticks.ToString();

调整分母允许您选择精度级别

I believe you can create a unix style datestamp accurate to a second using the following

//Find unix timestamp (seconds since 01/01/1970)
long ticks = DateTime.UtcNow.Ticks - DateTime.Parse("01/01/1970 00:00:00").Ticks;
ticks /= 10000000; //Convert windows ticks to seconds
timestamp = ticks.ToString();

Adjusting the denominator allows you to choose your level of precision

梦在深巷 2024-07-29 03:38:39

您可以使用 DateTime.Ticks 属性,它是一个长期且通用的可存储属性,始终在增长并且在紧凑型框架上也可用。 只需确保您的代码在 12 月 31 日 9999 之后不再使用即可;)

You could use the DateTime.Ticks property, which is a long and universal storable, always increasing and usable on the compact framework as well. Just make sure your code isn't used after December 31st 9999 ;)

任性一次 2024-07-29 03:38:39

您还可以使用

Stopwatch.GetTimestamp().ToString();

You can also use

Stopwatch.GetTimestamp().ToString();
满栀 2024-07-29 03:38:39

当您需要以秒为单位的时间戳时,可以使用以下命令:

var timestamp = (int)(DateTime.Now.ToUniversalTime() - new DateTime(1970, 1, 1)).TotalSeconds;

when you need in a timestamp in seconds, you can use the following:

var timestamp = (int)(DateTime.Now.ToUniversalTime() - new DateTime(1970, 1, 1)).TotalSeconds;
断桥再见 2024-07-29 03:38:39

如果您希望时间戳与实际时间相对应,但又希望它们是唯一的(对于给定的应用程序实例),您可以使用以下代码:

public class HiResDateTime
{
   private static long lastTimeStamp = DateTime.UtcNow.Ticks;
   public static long UtcNowTicks
   {
       get
       {
           long orig, newval;
           do
           {
               orig = lastTimeStamp;
               long now = DateTime.UtcNow.Ticks;
               newval = Math.Max(now, orig + 1);
           } while (Interlocked.CompareExchange
                        (ref lastTimeStamp, newval, orig) != orig);

           return newval;
       }
   }
}

If you want timestamps that correspond to actual real times BUT also want them to be unique (for a given application instance), you can use the following code:

public class HiResDateTime
{
   private static long lastTimeStamp = DateTime.UtcNow.Ticks;
   public static long UtcNowTicks
   {
       get
       {
           long orig, newval;
           do
           {
               orig = lastTimeStamp;
               long now = DateTime.UtcNow.Ticks;
               newval = Math.Max(now, orig + 1);
           } while (Interlocked.CompareExchange
                        (ref lastTimeStamp, newval, orig) != orig);

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