Solved

Can i convert a DateTime into a GUID?

Posted on 2014-02-10
3
1,168 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…

726 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