locked
How to convert the date from any other format to yyyy/mm/dd? RRS feed

  • Question

  • Hi all 

    I have set my system's date to mm/dd/yyyy format and have tried the  following code in VB.net 

    Dim JDATE As DateTime = DateTime.Now()
     JDATE = JDATE.ToString("yyyy'/'MM'/'dd", System.Globalization.CultureInfo.InvariantCulture)

    MessageBox.show(JDATE)

    On executing this I get the O/P in mm/dd/yyyy format. 

    Please help me solve this issue.

    Thanks


    sauthee


    • Edited by Sauthee Wednesday, January 11, 2017 10:37 AM
    Wednesday, January 11, 2017 10:36 AM

Answers

  • Hi Sauthee,

    Thank you for posting here.

    When you show the JDATE in MessageBox, the Date type value will be display. Because the system default time format is 'MM/dd/yyyy'. So I suggest that you could convert the Date time type to String type.

    Like this:

     Dim JDATE As DateTime = DateTime.Now()
            Dim JDATE1 As String = JDATE.ToString("yyyy/MM/dd", System.Globalization.CultureInfo.InvariantCulture)
    
            MessageBox.Show(JDATE1)

    More information from MSDN documentation: https://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx?f=255&MSPPError=-2147217396#dateSeparator

    Hope it helps.

    Best Regards,

    Neda Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Sauthee Friday, January 13, 2017 3:12 AM
    Thursday, January 12, 2017 4:36 AM

All replies

  • Hi

    Maybe I misunderstand your question, but if allyou want is to format the date, then .......

            Dim JDATE As DateTime = Now.Date
            '    JDATE = JDATE.ToString("yyyy'/'MM'/'dd", System.Globalization.CultureInfo.InvariantCulture)
    
            MessageBox.Show(JDATE & vbCrLf & JDATE.ToString("yyyy'/'MM'/'dd"))
    


    Regards Les, Livingston, Scotland

    Wednesday, January 11, 2017 11:06 AM
  • Hello,

    You don't set the format of an actual date, in this case JDATE back to JDATE as .ToString returns a string, not a Date. The formatting of a Date is for us to view and not for storage. When looking at a date say in SQL-Server Management Studio you see 2015-07-04 00:00:00.000 but the current culture of my machine in this case will always display 07/04/2015. We can change the culture for an application.

    Here are some examples

    Imports System.Globalization
    
    Public Class Form1
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim theDate = #07/04/2015#
            Console.WriteLine(theDate)
            Console.WriteLine(theDate.ToString("yyyy/MM/dd"))
            Console.WriteLine(CultureInfo.CurrentCulture.DateTimeFormat.DateSeparator)
            Application.CurrentCulture = New CultureInfo("fr-BE")
            Console.WriteLine(theDate)
            Console.WriteLine(CultureInfo.CurrentCulture.DateTimeFormat.DateSeparator)
    
            CultureInfo.CurrentCulture.DateTimeFormat.DateSeparator = "-"
            Console.WriteLine(theDate)
        End Sub
    End Class
    

    Results

    7/4/2015 12:00:00 AM
    2015/07/04
    /
    4/07/2015 00:00:00
    /
    4-07-2015 00:00:00
    

    You can't do this because there is no conversion and as mentioned above formatting is not for storage but for display.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Proposed as answer by Ashidacchi Wednesday, January 11, 2017 11:38 AM
    Wednesday, January 11, 2017 11:10 AM
  • Your first problem is that you do not have Option Strict On.  That is what allows you to assign a string variable to a date variable and show JDATE in a message box without a .ToString. 

    Select the Tools / Options menu.  Then select Projects and Solutions.  Then select VB Defaults.  Your settings should be

    Explicit - On

    Strict - On

    Compare - Binary

    Infer - Off

    In the long run you will be better off with these settings.

    After those changes your code will look like this,

            Dim JDATE As DateTime = DateTime.Now()
            MessageBox.Show(JDATE.ToString("yyyy/MM/dd"))
    


    "Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it." - MSDN User JohnWein    Multics - An OS ahead of its time.

    • Proposed as answer by Cor Ligthert Wednesday, January 11, 2017 12:22 PM
    • Unproposed as answer by Cor Ligthert Wednesday, January 11, 2017 2:03 PM
    Wednesday, January 11, 2017 12:04 PM

  • Your settings should be

    Explicit - On

    Strict - On

    Compare - Binary

    Infer - Off


    I only proposed your reply as answer because of the sample (I unproposed it again because of the Multics spam). The rest is real elitist fudget. 

    The options are made to give everybody the freedom to do as they wish. 

    I know that you like to order every body to do likewise you.

    But there  is not any reason to do that with this. 

    I prefer all Options On while the official Microsoft policy advice's in the default settings at least Option Explicit On and Option Infer On. 


    MessageBox.Show(Now.ToString("yyyy/MM/dd"))


    Success
    Cor




    • Edited by Cor Ligthert Wednesday, January 11, 2017 2:04 PM
    • Proposed as answer by Neda Zhang Thursday, January 12, 2017 4:15 AM
    • Unproposed as answer by KareninstructorMVP Friday, January 13, 2017 1:17 AM
    Wednesday, January 11, 2017 12:30 PM
  • Hi Sauthee,

    Thank you for posting here.

    When you show the JDATE in MessageBox, the Date type value will be display. Because the system default time format is 'MM/dd/yyyy'. So I suggest that you could convert the Date time type to String type.

    Like this:

     Dim JDATE As DateTime = DateTime.Now()
            Dim JDATE1 As String = JDATE.ToString("yyyy/MM/dd", System.Globalization.CultureInfo.InvariantCulture)
    
            MessageBox.Show(JDATE1)

    More information from MSDN documentation: https://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx?f=255&MSPPError=-2147217396#dateSeparator

    Hope it helps.

    Best Regards,

    Neda Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Sauthee Friday, January 13, 2017 3:12 AM
    Thursday, January 12, 2017 4:36 AM
  • Hi Sauthee,

    Thank you for posting here.

    When you show the JDATE in MessageBox, the Date type value will be display. Because the system default time format is 'MM/dd/yyyy'. So I suggest that you could convert the Date time type to String type.

    Like this:

     Dim JDATE As DateTime = DateTime.Now()
            Dim JDATE1 As String = JDATE.ToString("yyyy/MM/dd", System.Globalization.CultureInfo.InvariantCulture)
    
            MessageBox.Show(JDATE1)

    More information from MSDN documentation: https://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx?f=255&MSPPError=-2147217396#dateSeparator

    Hope it helps.

    Best Regards,

    Neda Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    @Neda, 

    Only in China, Japan, Arabia and some other countries mostly previously communist use the ISO date time as system presentation time. 

    https://en.wikipedia.org/wiki/Date_format_by_country

    It is better but in other countries almost nobody wanted to accept it.


    Success
    Cor


    Thursday, January 12, 2017 9:31 AM

  • I only proposed your reply as answer because of the sample (I unproposed it again because of the Multics spam). The rest is real elitist fudget. 

    The options are made to give everybody the freedom to do as they wish. 

    I know that you like to order every body to do likewise you.

    But there  is not any reason to do that with this. 

    I prefer all Options On while the official Microsoft policy advice's in the default settings at least Option Explicit On and Option Infer On. 


    MessageBox.Show(Now.ToString("yyyy/MM/dd"))


    Success
    Cor




    "What is your major malfunction, ..." , said Gunnery Sergeant Hartman. 

    You could have simply posted your objections to my choices for Option settings without mentioning me or the link to a 50+ year old OS. It could only be spam if I were trying to sell something or drive business to a competitor which isn't the case, so please stop. 

    Because of your personal attacks I find myself avoiding threads in which you are already participating.  I know for a fact that I am not your only target and I feel bad for them.  It is my opinion that you give threads you participate in a bad vibe more often than not. No where in the thread I posted above did I mention you in any way, good or bad.

    Apparently we only disagree about Infer which was there, as far as I can tell, to aid the porting of VB6 code. 

    The only time I have had the need to turn Infer On is for LINQ that produces Anonymous types.


    "Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it." - MSDN User JohnWein    Multics - An OS ahead of its time.


    • Edited by dbasnett Thursday, January 12, 2017 11:51 AM
    Thursday, January 12, 2017 11:50 AM
  • Thank you for your reply

    sauthee

    Friday, January 13, 2017 3:14 AM