C# 中创建时间戳的函数
我想知道,有没有办法在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我总是使用如下所示的内容:
这会给你一个像 200905211035131468 这样的字符串,因为该字符串从时间戳的最高位到最低位 如果你坚持的话,SQL查询中的简单字符串排序可用于按日期排序数据库中的值
I always use something like the following:
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
我相信您可以使用以下命令创建精确到秒的unix风格日期戳
调整分母允许您选择精度级别
I believe you can create a unix style datestamp accurate to a second using the following
Adjusting the denominator allows you to choose your level of precision
您可以使用 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 ;)
您还可以使用
You can also use
当您需要以秒为单位的时间戳时,可以使用以下命令:
when you need in a timestamp in seconds, you can use the following:
如果您希望时间戳与实际时间相对应,但又希望它们是唯一的(对于给定的应用程序实例),您可以使用以下代码:
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: