ToLongDateString remove the leading day

ToLongDateString remove the leading day (msdn.microsoft.com)

In Visual Basic 2010 using the .Net Framework 4.0, the ToLongDateString method converts the value of the current DateTime object to its equivalent long date string representation. The date value result using this method for example is: (Monday, December 19, 2011).
A normal request is to remove the day, and only show the Month, Day and Year (December 19, 2011) The best way to accomplish this, is to remove day by splitting the string.
Code Example:
‘Create the directory by date
Dim thedatestringtemp As String = String.Empty
Dim thedatestring As String = String.Empty
Dim thedateday As Date = Today
thedatestringtemp = RTrim(thedateday.ToLongDateString)

‘12192011 remove the day from ToLongDateString
‘Tuesday, November 29, 2011 to November 29, 2011

Dim P1Day As String = String.Empty
Dim P2MonthandDay As String = String.Empty
Dim P3Year As String = String.Empty

Dim line As String = String.Empty
Dim linearray() As String
line = RTrim(thedatestringtemp)

linearray = line.Split(“,”)

P1Day = linearray(0) ‘1
P2MonthandDay = linearray(1) ‘2
P3Year = linearray(2) ‘3

‘left trim to get rid of any leading spaces

thedatestring = LTrim(P2MonthandDay) & “,” & RTrim(P3Year)

‘end new 12192011

My Two Cents: Since the method ToLongDateString places a comma between the day, month and the year, we use the comma to split the string into 3 seperate strings. Then we simply reassemble the string, look at the final result thedatestring. Very easy!

7 thoughts on “ToLongDateString remove the leading day

  1. Nick,

    This is a terrible way of handling this problem. First, from your own link to the Microsoft site it says:

    Important note Important

    The string returned by the ToLongDateString method is culture-sensitive. It reflects the pattern defined by the current culture’s DateTimeFormatInfo object. For example, for the en-US culture, the standard long date pattern is “dddd, MMMM dd, yyyy”; for the de-DE culture, it is “dddd, d. MMMM yyyy”; for the ja-JP culture, it is “yyyy’?’M’?’d’?'”. The specific format string on a particular computer can also be customized so that it differs from the standard long date format string.

    The returned value is dependent upon the cultural settings and if someone changes that then your code will not work.

    You have many lines of code that can easily be satisfied with one line by using the string.format as explained here:

    http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx

    This way, no matter what, you will get a standard.

    Regards,
    Cory

    • Cory,

      Its good hearing from you! You were my favorite developer back in the Sierra Computers, Accela days in California! I can see why it looks terrible, but it has to do with the end result of an automation cloud based intergration of two very complex systems. The Microsoft way: the standard long date pattern is “dddd, MMMM dd, yyyy”; for the de-DE culture, it is “dddd, d. MMMM yyyy”; for the ja-JP culture, it is “yyyy’?’M’?’d’?’”. Does not work. The operating system will NOT recognize this setting in a service, and the automation will fail. Trust me, we attempted the Micorosft way the first time, and it failed on a Microsoft Server 2008 R2 System running in the cloud. So we this way to be a huge success in services. I wish the Microsoft way would have worked, because it is cleaner. BUT, when it doesn’t work, we think outside the box.

      Its good hearing from you!!

      Nick

      • How come you didn’t just do this:

        Dim todaysdate As Date = Date.Now()

        ‘ This will display the Long Date String
        TextBox1.Text = todaysdate.ToLongDateString()

        ‘ This will convert the date string to a custom date
        TextBox2.Text = todaysdate.ToString(“MMMM d, yyyy”)

          • This code is perfect if your working with dates and strings. In my crazy case we had to send an object attached in a stream (TCPIP) to a non-windows system. So one of my guys on my team came up with an idea to create an array list as an object, I thought it wouldn’t work…BUT IT DID. So we posted here. 🙂
            We use your code a lot for normal coding:
            Dim THEDATESTR As String = Format(DateTime.Now.ToString(“yyyyMMdd”)) ‘format for SAP
            But sometimes we get crazy problems where the normal way doesn’t work, and crazy code that you would never think would work…does… I tend to enjoy the crazy code, then talk about it and buy a round of beer for the developer that came up with it…

  2. Nick,

    There is nothing wrong with your code. We only allow objects and strings between our unix systems, not dates.Your solution worked perfectly for us.

    Mike P.

Leave a Reply

Your email address will not be published. Required fields are marked *