在 C# 中创建时间戳的函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/892074/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 08:41:27  来源:igfitidea点击:

Function that creates a timestamp in c#

c#timestampcompact-frameworkdatabase-agnostictime-precision

提问by Konstantinos

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).

我想知道,有没有办法从日期时间在 c# 中创建时间戳?我需要一个同样适用于 Compact Framework 的毫秒精度值(因为 DateTime.ToBinary() 在 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.

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

采纳答案by RobV

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

这将为您提供一个类似 200905211035131468 的字符串,因为该字符串从时间戳的最高位到最低位,SQL 查询中的简单字符串排序可用于按日期排序,如果您将值粘贴到数据库中

回答by Frans Bouma

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 ;)

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

回答by Voxel

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

我相信您可以使用以下方法创建精确到秒的 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();

Adjusting the denominator allows you to choose your level of precision

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

回答by Ian Mercer

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;
       }
   }
}

回答by mgokhanbakal

You can also use

你也可以使用

Stopwatch.GetTimestamp().ToString();

秒表.GetTimestamp().ToString();

回答by Adi_Pithwa

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;