Solved

Can i convert a DateTime into a GUID?

Posted on 2014-02-10
3
991 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 19

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

757 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

23 Experts available now in Live!

Get 1:1 Help Now