What is wrong with using DateTime.Now. as main part of Unique ID?

I used to use RNGCryptoServiceProvider to generate string-based Order ID’s, but, there were 4 instances where [email protected]$%^*()_- would generate an already-existing Order ID.

And it got me thinking…

Why can’t we just use something like:

<html>
...
<p>@GenerateOrderId()</p>
...
</html>

and:

public string GenerateOrderId()
{
   return "OID" + 
      DateTime.Now.Year + DateTime.Now.Month +
      DateTime.Now.Day + 
      DateTime.Now.Hour + 
      DateTime.Now.Minute + 
      DateTime.Now.Second + 
      DateTime.Now.Millisecond;
}

I’ve been told that it’s a bad idea, but without any explanation, or reason, it’s still stuck in my head.
… The reason I’d like to use this, is because it will always be unique.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Computers work at nanosecond speeds. You would be guaranteed to generate a duplicate order ID almost immediately.

Your best bet would be to use a GUID [MSDN-C#] [MSDN-SQL] (a.k.a. UNIQUEIDENTIFIER in the SQL world).

Method 2

It won’t always be unique.

If the same process is carried out during the same millisecond, then it will be identical.

As @Bill has stated, you should use a GUID for a unique string.

Method 3

Never call the Now getter that many times. “Now” may change while you’re adding the strings. You could say DateTime.Now.ToString("yyyyMMddHHmmssfff") or similar, but it’s much better to use Guid.NewGuid() for this.

Method 4

If you just want a globally unique identifier and aren’t bothered about the format, why don’t you just use a GUID?

http://msdn.microsoft.com/en-us/library/system.guid.newguid.aspx

using System;

class Sample 
{
    public static void Main() 
    {
        Guid g = Guid.NewGuid();
        Console.WriteLine(g);
    }
}

It even has a type in T-SQL (which you may well be using given that you’re using ASP.NET)

Method 5

I recommend letting your database handle that responsibility, but if you must do it in code, then use GUID. GUID has a low probability of being duplicated.

  public string GenerateOrderId()
  {
    return System.Guid.NewGuid().ToString();
  }

Method 6

Not to beat a dead horse, but your usage of DateTime.Now is of more concern than what you’re trying to do. You can rewrite your method and achieve the same goal much more succinctly:

public string GenerateOrderID()
{
  return "OID" + DateTime.Now.Ticks.ToString();
}

I would still recommend using a Guid over this approach. However, 99% of the time, the Ticks property is going to give you a different number each time it’s called.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x