在C#创建一个时间戳功能 [英] Function that creates a timestamp in c#

查看:184
本文介绍了在C#创建一个时间戳功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道,有没有一种方法来创建一个日期时间在C#中的时间戳?
我需要一毫秒precision值,也能在Compact Framework的(说既然DateTime.ToBinary()不存在CF)。

我的问题是,我想存储在一个数据库无关的方式这个值,所以我可以在以后sortby它,并找出哪些值是从另一等更大。


解决方案

我一直使用类似以下内容:

 公共静态字符串GetTimestamp(此日期时间值)
{
    返回value.ToString(yyyyMMddHHmmssfff);
}

这会给你一个字符串像200905211035131468,作为字符串从可以使用的日期,如果你在一个数据库中坚持值<责令时间戳最低阶简单的字符串在SQL查询排序的最高阶位的推移/ p>

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

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天全站免登陆