0

I am currently doing some code reviews and noticed that the following line is used thorought the system:

userDOB.ToString("g") --Outputs 15/12/2000 13:45

My issue with this is the magic string ("g").

I dont think/can't find the datetime object has a equivlent function e.g. ToShortDateString() (if it did i would find that more readable).

Is there a function on the datetime object that outputs in this format? Or do i have to create a custom extension method or go down some other path?

user1786107
  • 2,921
  • 5
  • 24
  • 35
  • 1
    Is the output what you want -- just without using the string "g" but by using a method or some other construct that reads better in code? – Kirk Woll Dec 02 '16 at 00:03
  • `.ToString("g")` generates a 'date' using the combined `d` (short date) and `t` (short time) specifiers. There is no `DateTime` extension method that does that so you will need to create you own extension method –  Dec 02 '16 at 00:48
  • 1
    `ToShortDateString()` is as good as saying `userDOB.ToString("d")`. It is just that few of the date and time formats are more likely to be used so .Net team has also provided some additional APIs. I'm not sure why do you think that `userDOB.ToString("g")` is less readable and you want to replace it with some extension method which doesn't have a parameter. My idea would be that APIs are what they are. As a developer while reading the code every time I'll have to go inside your extension method to understand what it is returning under the hood. – RBT Dec 02 '16 at 01:02
  • [Sorry got restricted by comment maximum capacity.] Write an extension method ONLY if you are doing something new which isn't fulfilled by the existing APIs otherwise you are giving just a new name to an existing functionality which is essentially bloating the Date-time class. In my perspective `userDOB.ToString("d")` is much more cleaner and clearer than it getting replaced with an alias extension name. Imagine the number of extension methods if you start exposing all the date time formats by a new name. There are at least 2 dozen date time formats. – RBT Dec 02 '16 at 01:06
  • [Sorry got restricted by comment maximum capacity.] Also, just for a sweet name u will be changing your entire source code. If whole of your source code is sharing a common date time format e.g. `"g"` then better declare a constant in your common project something like `public static const string GENERAL_DATE_TIME_FORMAT = "g"` and use it everywhere. A constant name is pretty self explanatory. So instead of going to MSDN site and seeing what "g" format emits you can just leave a sample output as comment in the file where your constant is kept. Think clean code! – RBT Dec 02 '16 at 01:10
  • Possible duplicate of [C# DateTime to "YYYYMMDDHHMMSS" format](http://stackoverflow.com/questions/3025361/c-sharp-datetime-to-yyyymmddhhmmss-format) – garethb Dec 02 '16 at 03:53
  • 1
    The most idiomatic way for a programmer to indicate that they want to convert a DateTime to a *standard* generic date/time string is via `.ToString("g")`. Anything you introduce is just going to add cognitive overhead and confusion. – Damien_The_Unbeliever Dec 02 '16 at 07:44

4 Answers4

1

I think you may be looking for the following:

userDOB.ToString("dd/MM/yyyy HH:mm")
Chris Cruz
  • 1,829
  • 13
  • 15
1

Is it possible to specify a custom date format instead of using ToString(“g”)?

Here is one possible answer. It converts to time in following format:

day/month/year hours:minutes

 var dateTime = DateTime.Now.ToString("dd/MM/yyyy HH:mm");

Is there a function on the datetime object that outputs in this format?

There are similar methods like ToShortDateString() but it is still not same format like you described.

Do I have to create a custom extension method or go down some other path?

Based on my personal opinion you can create extension method with more descriptive name, but I rather like to declare some constant variable which hold value about your specific format and populate that variable from e.g. config file.

var dateTime = DateTime.Now.ToString(_someMoreDescriptiveDateTimeFormat);

Maybe good idea for extension method method can be like this:

        var dateTimeString = DateTime.Now.ToFormattedString();

And here is implementation of method which is documented with explanatory data. It can also be helpfull during review.

        /// <summary>
        /// Convert DateTime object into specially formated string.
        /// </summary>
        /// <param name="dateTime">DateTime which will be converted.  </param>
        /// <returns>Custom date and time string.</returns> 
        public static string ToFormattedString(this DateTime dateTime)
        {
            return dateTime.ToString("dd/MM/yyyy HH:mm");
        }
kat1330
  • 5,134
  • 7
  • 38
  • 61
0

There are two whole pages on DateTime formatting on MSDN. There's way too much to put here.

Standard formats (your 'g' will be shown here): https://msdn.microsoft.com/en-us/library/az4se3k1(v=vs.110).aspx

Custom: https://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110).aspx

Xavier J
  • 4,326
  • 1
  • 14
  • 25
0

this is how it works

String.Format("{0:d/M/yyyy HH:mm:ss}", userDOB);

sample output is

// "9/3/2008 16:05:07" - english (en-US)