Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Can i convert a DateTime into a GUID?

Posted on 2014-02-10
3
Medium Priority
?
1,467 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 1000 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 1000 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

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Question has a verified solution.

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

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…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

596 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