none
Format versus ToString RRS feed

  • Question

  • What is the difference between the "format" command and "ToString" command when I am using them to format dates. Getting the same output of "01 May 2018" for both cases for the following piece of code.

    Dim someDate As Date = #2018-05-01# 'YYYY-MM-DD
    Console.WriteLine("The formatted date is: {0}", Format(someDate, "dd MMM yyyy"))
    Console.WriteLine("The formatted date is: {0}", someDate.ToString("dd MMM yyyy"))
    Console.ReadLine()


    Sougata Ghosh

    Tuesday, May 1, 2018 6:02 AM

Answers

  • There is no difference just preference. 

    The format has always been in VB and is also in VBS and VBA and also available as method in .Net.


    The ToString is a typical OOP property  of the base class "Object" which is mainly created to be used overloaded for this kind of things. 

    My preference is to use ToString because it are less characters to type and that is all. 

    There is no noticeable speed difference for instance or difference in size in the assembly. 

    https://msdn.microsoft.com/en-us/library/system.object.tostring(v=vs.110).aspx


    Success
    Cor

    • Marked as answer by sougata12 Tuesday, May 1, 2018 8:21 AM
    Tuesday, May 1, 2018 8:14 AM
  • If you are interested, you can investigate the third variant too:

       Console.WriteLine("The formatted date is: {0:dd MMM yyyy}", someDate)

    According to the sources of WriteLine, the third variant invokes someDate.ToString.


    Tuesday, May 1, 2018 8:36 AM

All replies