Solved

Can i convert a DateTime into a GUID?

Posted on 2014-02-10
3
1,050 Views
Last Modified: 2014-02-12
I have an interface which has a Guid as an Id field.
If I implement the interface in a class that represents dates, if I have an instance of the class is it possible to convert a datetime value into a guid so that it can be used as the unique identifier? I know I could use the datetime directly (as it would be unique in this setting) but I want to be able to satisfy the requirements of the interface.
0
Comment
Question by:quentinA
3 Comments
 
LVL 40

Assisted Solution

by:Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger) earned 250 total points
ID: 39848582
You can use the following Guid constructor: http://msdn.microsoft.com/en-us/library/3573497s(v=vs.110).aspx

Pass it the components of your date (milliseconds, year, day, month, hour, seconds) as you see fit to initialize it.
0
 
LVL 20

Accepted Solution

by:
Daniel Van Der Werken earned 250 total points
ID: 39848618
Here is a simplistic implementation. The only thing you could do differently is ensure any days or months there were single digit (like 2 for February) is 02 instead of 2. However, this will put the date in the beginning of the Guid. You can modify this if you need hours, minutes, seconds, etc.
        public static Guid DateTimeToGuid(DateTime dateTime)
        {
            Guid guid = Guid.NewGuid();
            string guidEnd = guid.ToString().Substring(guid.ToString().IndexOf("-"), guid.ToString().Length - guid.ToString().IndexOf("-"));
            string guidStart = dateTime.Day.ToString() + dateTime.Month.ToString() + dateTime.Year.ToString();
            guidStart = guidStart.PadRight(8, '0');
            guid = new Guid(guidStart + guidEnd);

            return guid;
        }

Open in new window

0
 

Author Closing Comment

by:quentinA
ID: 39852963
Thanks both.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
coded character set iptc field within a jpeg 1 70
In WPF / C# binding a local database in code behind 1 24
Export import database 4 43
Javascript and Jquery not firing 9 42
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now