热门标签 | 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;

推荐阅读
  • 本文详细介绍了 Java 中 `java.sql.Timestamp` 类的 `valueOf()` 方法,该方法用于将符合 JDBC 转义格式的时间戳字符串转换为 `Timestamp` 对象,并通过示例展示了其在实际开发中的应用。 ... [详细]
  • 本文详细介绍了Java中org.neo4j.helpers.collection.Iterators.single()方法的功能、使用场景及代码示例,帮助开发者更好地理解和应用该方法。 ... [详细]
  • 优化ListView性能
    本文深入探讨了如何通过多种技术手段优化ListView的性能,包括视图复用、ViewHolder模式、分批加载数据、图片优化及内存管理等。这些方法能够显著提升应用的响应速度和用户体验。 ... [详细]
  • 本文详细介绍了 Apache Jena 库中的 Txn.executeWrite 方法,通过多个实际代码示例展示了其在不同场景下的应用,帮助开发者更好地理解和使用该方法。 ... [详细]
  • 本文详细探讨了JDBC(Java数据库连接)的内部机制,重点分析其作为服务提供者接口(SPI)框架的应用。通过类图和代码示例,展示了JDBC如何注册驱动程序、建立数据库连接以及执行SQL查询的过程。 ... [详细]
  • 本文详细介绍了优化DB2数据库性能的多种方法,涵盖统计信息更新、缓冲池调整、日志缓冲区配置、应用程序堆大小设置、排序堆参数调整、代理程序管理、锁机制优化、活动应用程序限制、页清除程序配置、I/O服务器数量设定以及编入组提交数调整等方面。通过这些技术手段,可以显著提升数据库的运行效率和响应速度。 ... [详细]
  • ElasticSearch 集群监控与优化
    本文详细介绍了如何有效地监控 ElasticSearch 集群,涵盖了关键性能指标、集群健康状况、统计信息以及内存和垃圾回收的监控方法。 ... [详细]
  • 本文详细探讨了Laravel框架中的数据库操作,包括读写分离、事务处理、Eloquent ORM的使用、关联关系管理及性能优化技巧。 ... [详细]
  • 本文深入探讨了Linux MMC框架中的Host对象,详细介绍了其核心数据结构和API,旨在为理解和开发MMC设备驱动提供指导。 ... [详细]
  • Explore how Matterverse is redefining the metaverse experience, creating immersive and meaningful virtual environments that foster genuine connections and economic opportunities. ... [详细]
  • Explore a common issue encountered when implementing an OAuth 1.0a API, specifically the inability to encode null objects and how to resolve it. ... [详细]
  • 本文探讨了在使用阿里云RDS实例时遇到的一个时区问题。该问题导致系统时间与预期时间相差13小时。通过深入分析,发现问题是由于名为CST的时区存在多种解释,特别是在MySQL和Java之间进行时区协商时出现的误解。 ... [详细]
  • 本文提供了关于如何在 Java 中使用 `com.amazonaws.services.kinesis.model.StreamDescription.getRetentionPeriodHours()` 方法的详细说明,并附带了多个实际代码示例。 ... [详细]
  • 本文介绍了jsoncpp,一个强大的C++库,专注于JSON数据的解析和生成。文章提供了详细的使用示例和常见问题的解决方案。 ... [详细]
  • Shiro功能拓展:登录失败重试次数限制
    本文详细介绍了如何在Apache Shiro框架中实现对用户登录失败重试次数的限制,通过自定义密码匹配器来增强系统的安全性。该方法不仅能够有效防止暴力破解攻击,还能确保合法用户的账户安全。 ... [详细]
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社区 版权所有