-3

Hey I'm using a Randomdate function in C# (it was written by another person here on StackOverflow - thank you :) ) Anyway it returns a random date between two dates, but in this format

the code:

public static DateTime RandomDay()
{
    DateTime start = new DateTime(2006, 1, 1);
    DateTime end = new DateTime(2013, 12, 31);

    Random gen = new Random();

    int range = (end - start).Days;
    return start.AddDays(gen.Next(range));
}

However, this returns a date in a format like 2008-10-25, however, i want the dates to represented like this:

25.10.2008:00:00.000

Is this possible? Thanks

Habib
  • 219,104
  • 29
  • 407
  • 436
Mefhisto1
  • 2,188
  • 7
  • 34
  • 73
  • You can specify locales to which dates are formatted. – ThaMe90 May 22 '13 at 09:17
  • This has nothing to do with the given method. Simply use one of the ToString overloads to format the `DateTime`. – Caramiriel May 22 '13 at 09:17
  • Duplicate question see: http://stackoverflow.com/questions/3025361/c-sharp-datetime-to-yyyymmddhhmmss-format – Amicable May 22 '13 at 09:19
  • I find your lack of research disturbing. ([first result on Google](http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx) for "format date C#") – Nasreddine May 22 '13 at 09:22
  • -1, I did a [Google search](https://www.google.com/webhp?sourceid=chrome-instant&ion=1&ie=UTF-8#hl=en&sclient=psy-ab&q=Change%20datetime%20format%20in%20C%23&oq=&gs_l=&pbx=1&fp=a50db3676f095c3b&ion=1&bav=on.2,or.r_cp.r_qf.&bvm=bv.46751780,d.bGE&biw=1920&bih=1139) on your question title, [this is what I got as the first result](http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx) – Nolonar May 22 '13 at 09:23

6 Answers6

5

Call ToString() on the date and pass your desired format.

var formatted = date.ToString("dd.MM.yyyy:HH:mm.fff");
Oliver
  • 8,794
  • 2
  • 40
  • 60
3

Have a look at the MSDN documentation page:

Custom date and time format

string formattedDate = DateTime.Now.ToString("dd.MM.yyyy:HH:mm:ss:fff");
Habib
  • 219,104
  • 29
  • 407
  • 436
Bidou
  • 7,378
  • 9
  • 47
  • 70
0

could you try this:

string urdate = RandomDay().ToString("dd.MM.yyyy:HH:mm.fff");
iceheaven31
  • 877
  • 1
  • 13
  • 23
0

DateTimes don't have a format. Instead, they store the date and time as the number of 100 nanosecond ticks since 12:00 midnight, January 1, 0001 A.D. inside a field of type long.

When you convert them to a string, then that string does have a format.

So what you need to do is to specify the format that you want as a parameter to a call to DateTime.ToString().

In your case, the correct format is specified by "dd.MM.yyyy:HH:mm.fff", so you can achieve what you want by doing:

DateTime myDate = RandomDay();
string myFormattedDateString = myDate.ToString("dd.MM.yyyy:HH:mm.fff");

See here for information about DateTime.ToString()

See here for information about the custom DateTime formatting strings such as the ones used in this answer.

Matthew Watson
  • 104,400
  • 10
  • 158
  • 276
0

You can try this

string urdate = RandomDay().Now.ToShortDateString();
Randhi Rupesh
  • 14,650
  • 9
  • 27
  • 46
0

You can supply a format string, such as:

RandomDay().ToString("dd.MM.yyyy:hh.mm.sss")

See Custom Date Time Format Strings

jongillster
  • 23
  • 1
  • 7