Parse Ticks to Datetime

Format from ticks to date

If logText is a string, you can convert it to long (Int64) and use this constructor:

DateTime date = new DateTime(long.Parse(logText));

How to convert ticks to datetime in Python?

The following will convert the ticks to a Python datetime object (from now) using datetime's timedelta.

import datetime
ticks = 52707330000
converted_ticks = datetime.datetime.now() + datetime.timedelta(microseconds = ticks/10)

Then something like:

converted_ticks.strftime("%Y-%m-%d %H:%M:%S") // '2015-08-07 14:17:48'

Hope this helps!

EDIT: Using just datetime.timedelta(microseconds = ticks/10) will give you the time, not relative to "now".

Convert ticks to Datetime

As Akos indicated you can pass ticks to the DateTime constructor then use the .net ToString method to format the string, eg

@output =
SELECT new DateTime(LastModifiedUtcDateTimeTicks).ToString("dd/MM/yyyy HH:mm:ss") AS ProcessedTime
FROM @extractVariable;

Parse ticks to datetime

Per the Ruby docs:

myTime = Time.at(1298011537289)

or since you're using milliseconds rather than seconds:

myTime = Time.at(1298011537289 / 1000)

But that will only remove sub-second precision, to retain it:

myTime = Time.at(Rational(1298011537289, 1000))

Difference in comparing DateTime and parsed DateTimeOffset ticks

In this case, it seems the problem is the format of the DateTime:

"yyyy-MM-ddThh:mm:ss.fffZ"  // lower hh

whereas, it should be:

"yyyy-MM-ddTHH:mm:ss.fffZ"  // upper HH

Using 12 hour (hh) format rather than 24 hour (HH) format would cause a 12 hour difference 50% of the time!


As Hans mentions in the comments, you should ensure you're using UTC to compare the DateTime values. DateTime.Parse by default sets the DateTime kind to DateTimeKind.Local. Is the client sending a UTC DateTime?

Update the client timestamp so it sends UTC DateTime with ToUniversalTime:

var clientTimestamp = DateTimeOffset.Now.ToUniversalTime();

Instead of DateTime.Parse consider DateTime.ParseExact with AdjustToUniversal:

// convert the date to UTC before saving to database
var utcServerDate = DateTime.ParseExact(jsonDateTime, "yyyy-MM-ddTHH:mm:ss.fffZ",
CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal);

And when comparing the DateTimeOffset.Now value use UtcTicks:

var nowDateTimeOffset = DateTimeOffset.Now.AddMinutes(-30).UtcTicks;

Converting ticks to DateTime

Your value doesn't seem to be a number of ticks; I suspect it's a UNIX timestamp (number of seconds since 1970/01/01 UTC)

Here's a function to convert from a UNIX timestamp:

static readonly DateTime _unixEpoch =
new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);

public static DateTime DateFromTimestamp(long timestamp)
{
return _unixEpoch.AddSeconds(timestamp);
}

c# reproduce datetime from ticks

It seems to me that your "ticks 62030" is actually "milliseconds 62030" in which case it's very simple - you just need to multiply by "the number of ticks per millisecond" which is 10,000. You don't need to use DateTime for this at all:

// Note that if you want any significant length of time, you'd expect to get
// the data as a long, not an int
int data = 62030; // Milliseconds
long ticks = data * 10000L;

... and you certainly don't need string conversions. Converting to a string, padding, and then converting back again is a very tortuous and error-prone way of performing multiplication.

Or if you do need a DateTime:

int data = 62030; // Milliseconds
long dateTime = new DateTime(data * 10000L);

I strongly suspect that any DateTime value that early should actually be treated as a TimeSpan though - what's this really meant to represent? If so, it's even easier:

TimeSpan ts = TimeSpan.FromMilliseconds(data);

Date and time concepts are very easy to mix up, and you end up with some very subtle bugs. Personally I'd recommend using my Noda Time project which separates them more than .NET does, but even if you don't use the library it's worth looking at the list of concepts so you can think about them appropriately within .NET too.

How to get date ticks in a javaScript?

  • JavaScript: Date.getTime(): a number, representing the number of milliseconds since midnight January 1, 1970.

  • C#: A single tick represents one hundred nanoseconds or one ten-millionth of a second. There are 10,000 ticks in a millisecond, or 10 million ticks in a second.

So, JavaScript ticks = (C# ticks / 10000) and your code looks OK. You just need to account for the difference in whichever code (C# or JavaScript) you choose.

If you take (an unambiguous day-month date) in JavaScript (e.g. 23 December 2018):

var $input = "23.12.2018";
var from = $input.split(".");

var dNew = new Date(from[2], from[1]-1, from[0]);
//Note dStart has 0 as month because JavaScript dates start at 0 and end with 11
var dStart = new Date(1970, 0, 1);
var seconds = dNew.getTime();
var secondsStart = dStart.getTime();
var dateDifference = seconds - secondsStart;
// multiply by 10000 to reconcile to c#
console.log("===> " + dateDifference * 10000);

Both answers come to 15455232000000000 if you use StringDateToDecimal("23 december 2018"); in your C# code.

I hope I'm getting the jist of what you are asking.



Related Topics



Leave a reply



Submit