5

I have a TimeUuid which I need to convert to a DateTime.

Is there a way to do this in C#?

I am using a CassandraCsharpDriver from DataStax, but it does not have the capability to convert the TimeUuid to Datetime, but the opposite is possible.

Example UUID: d91027c5-bd42-11e6-90be-2b4914b28d57

Manfred Radlwimmer
  • 13,257
  • 13
  • 53
  • 62
KhanS
  • 1,167
  • 2
  • 12
  • 27

2 Answers2

4

You can use the GetDate() method of the TimeUuid structure, that returns the DateTimeOffset representation of the value.

TimeUuid uuidValue = row.GetValue<TimeUuid>("time");
// TimeUuid structure exposes GetDate() method
DateTimeOffset dateOffset = uuidValue.GetDate();
DateTime dateTime = dateOffset.DateTime;
jorgebg
  • 6,560
  • 1
  • 22
  • 31
1

You can use these 2 helpers and convert to datetime using the GetDateTime method,

GuidVersion.cs

file-guidgenerator-cs

Code:

  String uuidString = "28442f00-2e98-11e2-0000-89a3a6fab5ef";
  Guid gui = new Guid(uuidString);
  DateTime dt = GuidGenerator.GetDateTime(gui);
Sajeetharan
  • 216,225
  • 63
  • 350
  • 396
  • 1
    I think the OP asked about a method to convert a `TimeUuid` into `DateTime` using the DataStax driver, your answer refers to FluentCassandra driver. – jorgebg Dec 13 '16 at 09:01
  • I try to revere `dt` to `uuidString` but got new guid, not like `28442f00-2e98-11e2-0000-89a3a6fab5ef`. what's problem ? – Сергей Sep 18 '18 at 14:59