Solved

Can i convert a DateTime into a GUID?

Posted on 2014-02-10
3
1,126 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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 video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

828 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