-3

In my test I have to fill the application number for that I already have guid method to generate random numbers because for each time while running the test it should generate different numbers.

var DocumentNo = Guid.NewGuid().ToString();
editCasePage.FillDocumentNumber(DocumentNo);

but I need document number in time stamp format.How can i do that? Thanks in advance!

Mutu Arron
  • 71
  • 1
  • 9
  • 2
    Your question is unclear. Please clarify what you want. Are you saying you want the document number to be an ISO8601 timestamp? Would `DateTime.UtcNow.ToString("o")` not suffice? – ProgrammingLlama Jan 10 '18 at 10:49
  • @Mutu Arron a random number will give you, ex. 10, so this number will represent what, your question is unclear – styx Jan 10 '18 at 10:51
  • Please provide an example of your input and what your expected output. – MakePeaceGreatAgain Jan 10 '18 at 10:51
  • 2
    1. `DocumentNo` is already a string. No point of calling it's `ToString()` method. 2. What is a *time stamp format*? You can simply generate a random number (using in instance of the `Random` class) and add it to `DateTime.Now` and then use `ToString("yyyy-MM-dd HH:mm:ss")` – Zohar Peled Jan 10 '18 at 10:51
  • 2
    If you already have a Guid, why not just use it instead of messing with some other format? – DavidG Jan 10 '18 at 10:52
  • @john Exactly what i am looking for i need an alternative option for guid. – Mutu Arron Jan 10 '18 at 10:57
  • I can´t imagine how a guid like `"F9168C5E-CEB2-4faa-B6BF-329BF39FA1E4"` should be transofmred to a timestamp or a date at all. – MakePeaceGreatAgain Jan 10 '18 at 10:59
  • @HimBromBeere: Take a look at my answer. ;-) – Oliver Jan 10 '18 at 11:20
  • 1
    Possible duplicate of [Random date in C#](https://stackoverflow.com/questions/194863/random-date-in-c-sharp) – JeffC Jan 11 '18 at 21:43

1 Answers1

1

If you need a random date then create one. One simple solution would be:

using System;

public class Program
{
    public static void Main()
    {
        for(int i = 0; i <= 100; i++)
        {
            var guid = Guid.NewGuid();
            var bytes = guid.ToByteArray();
            var rawValue = BitConverter.ToInt64(bytes, 0);
            var inRangeValue = Math.Abs(rawValue) % DateTime.MaxValue.Ticks;
            var date = new DateTime(inRangeValue);

            Console.WriteLine(date);
        }
    }
}
Oliver
  • 43,366
  • 8
  • 94
  • 151
  • It's worth noting that this method does not guarantee unique results in any way. using a GUID [practically guarantees uniqueness.](https://stackoverflow.com/questions/1155008/how-unique-is-uuid) (though not completely guarantees it). – Zohar Peled Jan 10 '18 at 11:03
  • A guid has 128 bit and a datetime has 64 bit. So you always have duplicates when you try to convert guids to datetimes. – Oliver Jan 10 '18 at 11:07
  • While your comment is correct, I didn't say *convert guids to datetimes.* I only pointed out the fact that `Guid.NewGuid()` would return a unique value, while `random.Next` does not guarantee it in any way. – Zohar Peled Jan 10 '18 at 11:10
  • That's right. Just to get more uniqueness, I changed my answer to convert a guid (partially) into a DateTime. ;-) – Oliver Jan 10 '18 at 11:19