热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

在c#中创建时间戳的函数-Functionthatcreatesatimestampinc#

Iwaswondering,isthereawaytocreateatimestampinc#fromadatetime?Ineedamillisecondpr

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中工作(因为在CF中不存在DateTime.ToBinary())。

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.

我的问题是我想以数据库不可知的方式存储这个值,所以我可以稍后对它进行排序,并找出哪个值来自另一个等等。

6 个解决方案

#1


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查询中可用于按日期排序,如果您在数据库中保留值

#2


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

调整分母可让您选择精确度

#3


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日之后未使用;)

#4


You can also use

你也可以使用

Stopwatch.GetTimestamp().ToString();

#5


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

#6


when u need in seconds:

什么时候你需要:

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

推荐阅读
author-avatar
mobiledu2502891413
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有