Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function that creates a timestamp in c#

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.

like image 564
Konstantinos Avatar asked May 21 '09 09:05

Konstantinos


People also ask

How does time () function work in C?

time() function in C The time() function is defined in time. h (ctime in C++) header file. This function returns the time since 00:00:00 UTC, January 1, 1970 (Unix timestamp) in seconds. If second is not a null pointer, the returned value is also stored in the object pointed to by second.

Is there a time function in C?

C library function - time() The C library function time_t time(time_t *seconds) returns the time since the Epoch (00:00:00 UTC, January 1, 1970), measured in seconds. If seconds is not NULL, the return value is also stored in variable seconds.

What is time_t type in C?

The time_t datatype is a data type in the ISO C library defined for storing system time values. Such values are returned from the standard time() library function. This type is a typedef defined in the standard <time.

What is timestamp in programming?

A timestamp is the current time of an event that a computer records. Through mechanisms, such as the Network Time Protocol, a computer maintains accurate current time, calibrated to minute fractions of a second. Such precision makes it possible for networked computers and applications to communicate effectively.


1 Answers

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

like image 144
RobV Avatar answered Sep 21 '22 06:09

RobV