Function that creates a timestamp in c#(在 C# 中创建时间戳的函数)
问题描述
我想知道,有没有办法在 c# 中从日期时间创建时间戳?我需要一个同样适用于 Compact Framework 的毫秒精度值(因为 DateTime.ToBinary() 在 CF 中不存在).
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.
推荐答案
我总是使用类似下面的东西:
I always use something like the following:
public static String GetTimestamp(this DateTime value)
{
return value.ToString("yyyyMMddHHmmssfff");
}
这将为您提供一个类似 200905211035131468 的字符串,因为该字符串从时间戳的最高位到最低位的字符串在您的 SQL 查询中进行简单的字符串排序,如果您在数据库中粘贴值,则可以使用按日期排序
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
这篇关于在 C# 中创建时间戳的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 C# 中创建时间戳的函数


- 为什么 C# 中的堆栈大小正好是 1 MB? 2022-01-01
- 是否可以在 .Net 3.5 中进行通用控件? 2022-01-01
- 使用 rss + c# 2022-01-01
- 带问号的 nvarchar 列结果 2022-01-01
- Windows 喜欢在 LINUX 中使用 MONO 进行服务开发? 2022-01-01
- 在 C# 中异步处理项目队列 2022-01-01
- 在 LINQ to SQL 中使用 contains() 2022-01-01
- Azure Active Directory 与 MVC,客户端和资源标识同一 2022-01-01
- C# 通过连接字符串检索正确的 DbConnection 对象 2022-01-01
- CanBeNull和ReSharper-将其用于异步任务? 2022-01-01