在 C# 中创建时间戳的函数 [英] Function that creates a timestamp in c#

查看:25
本文介绍了在 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# 中创建时间戳的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆