none
Date Conversion format mismatch RRS feed

  • Question

  • I have a .Net 4.5 website on IIS 6 running on Windows Server 2012 R2.
     When converting a date in  format MM/dd/YYYY ,c# converted into MM-dd-YYYY format
    with en-IN culture.
    Friday, March 1, 2019 10:49 AM

All replies

  • When i converting date in 'dd/MM/yyyy'  format or other format which contains '/' has date converted in 'dd-MM-yyyy' format.

    when i select date,01-02-2019 and dd/MM/yyyy format, c# convert date in dd-MM-yyyy means 01-Feb-2019


    Friday, March 1, 2019 10:30 AM
  • Please provide code examples of what you're talking about. The culture is determined by the code doing the parsing unless you're specifying a culture. If you only support fixed format dates then use ParseExact instead of Parse.

    Michael Taylor http://www.michaeltaylorp3.net

    Friday, March 1, 2019 2:56 PM
    Moderator
  • Hi KISHOR,

    Thank you for posting here.

    For your question, you want to convert date in 'dd/MM/yyyy'  format into 'dd-MM-yyyy' format with en-IN culture.

    You could try the following code.

    DateTime date = DateTime.Now;
                string d1 = date.ToString("dd/MM/yyyy");//04/03/2019
                CultureInfo cultureInfo = new CultureInfo("en-IN");
                var date1 = DateTime.Parse(d1, cultureInfo);
                string d2 = date1.ToString("dd-MM-yyyy");//04-03-2019
    

    Hope my solution could be helpful.

    Best regards,

    Jack



    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.

    Monday, March 4, 2019 7:35 AM
    Moderator
  • Hi

    Is your problem solved? If so, please post "Mark as answer" to the appropriate answer, so that it will help other members to find the solution quickly if they face a similar issue.

    Best Regards,

    Jack


    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.


    Tuesday, March 5, 2019 9:01 AM
    Moderator
  • code is:

    CultureInfo culture = new CultureInfo("en-IN");

    DateTime dt = Convert.ToDateTime("02-01-2019", culture);

    string result dt.ToString("dd/MMM/yyyy");

    result is 02-Jan-2019 

    Expected result is 02/Jan/2019 

    Thursday, March 7, 2019 10:17 AM
  • code is: 
    CultureInfo culture = new CultureInfo("en-IN"); 
    DateTime dt = Convert.ToDateTime("02-01-2019", culture); 
    string result dt.ToString("dd/MMM/yyyy"); 
    Current result is 02-Jan-2019  
    Expected Result is 02/Jan/2019  
    Thursday, March 7, 2019 10:19 AM
  • Hi KISHOR,

    Thanks for feedback.

    I have tested your code, I found that the Current result is 02/Jan/2019 instead of 02-Jan-2019. So I think that your solution is correct. You could refer to the following picture.

    Best Regards,

    Jack


    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.


    Friday, March 8, 2019 3:27 AM
    Moderator
  • I have tested your code and also my code is working and getting expected result as per date format on my windows server 2008 r2 but on client windows server 2008 r2 machine,expected result not getting.
    Friday, March 8, 2019 5:01 AM
  • Hi KISHOR,

    <<I have a .Net 4.5 website on IIS 6 running on Windows Server 2012 R2.

    <<but on client windows server 2008 r2 machine,expected result not getting.

    For your question,I want to know what your current operating environment is. Is windows server 2008 r2, or windows server 2012 r2?

    Best Regards,

    Jack


    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.

    Friday, March 8, 2019 5:31 AM
    Moderator
  • current operating environment  is windows server 2012 r2 on my local machine and client machine

    but in my environment its working..

    Friday, March 8, 2019 10:46 AM
  • when i trying to convert date in following format

    dd/MM/yy
    dd/MMM/yy
    dd/MM/yyyy
    dd/MMM/yyyy
    MM/dd/yy
    MMM/dd/yy
    MM/dd/yyyy
    MMM/dd/yyyy
    MMM/yyyy
    yy/MMM
    yyyy/MMM
    yy/MM/dd
    yy/MMM/dd
    yyyy/MM/dd
    yyyy/MMM/dd

    date is converted into  following format

    dd-MM-yy
    dd-MMM-yy
    dd-MM-yyyy
    dd-MMM-yyyy
    MM-dd-yy
    MMM-dd-yy
    MM-dd-yyyy
    MMM-dd-yyyy
    MMM-yyyy
    yy-MMM
    yyyy-MMM
    yy-MM-dd
    yy-MMM-dd
    yyyy-MM-dd
    yyyy-MMM-dd

    but i am expecting result in following format with en-IN Culture..

    dd/MM/yy
    dd/MMM/yy
    dd/MM/yyyy
    dd/MMM/yyyy
    MM/dd/yy
    MMM/dd/yy
    MM/dd/yyyy
    MMM/dd/yyyy
    MMM/yyyy
    yy/MMM
    yyyy/MMM
    yy/MM/dd
    yy/MMM/dd
    yyyy/MM/dd
    yyyy/MMM/dd
    Saturday, March 9, 2019 1:07 PM
  • when i trying to convert date in following format

    dd/MM/yy
    dd/MMM/yy
    dd/MM/yyyy
    dd/MMM/yyyy
    MM/dd/yy
    MMM/dd/yy
    MM/dd/yyyy
    MMM/dd/yyyy
    MMM/yyyy
    yy/MMM
    yyyy/MMM
    yy/MM/dd
    yy/MMM/dd
    yyyy/MM/dd
    yyyy/MMM/dd

    date is converted into  following format

    dd-MM-yy
    dd-MMM-yy
    dd-MM-yyyy
    dd-MMM-yyyy
    MM-dd-yy
    MMM-dd-yy
    MM-dd-yyyy
    MMM-dd-yyyy
    MMM-yyyy
    yy-MMM
    yyyy-MMM
    yy-MM-dd
    yy-MMM-dd
    yyyy-MM-dd
    yyyy-MMM-dd

    but i am expecting result in following format with en-IN Culture..

    dd/MM/yy
    dd/MMM/yy
    dd/MM/yyyy
    dd/MMM/yyyy
    MM/dd/yy
    MMM/dd/yy
    MM/dd/yyyy
    MMM/dd/yyyy
    MMM/yyyy
    yy/MMM
    yyyy/MMM
    yy/MM/dd
    yy/MMM/dd
    yyyy/MM/dd
    yyyy/MMM/dd

    code is:

    CultureInfo culture = new CultureInfo("en-IN");

    DateTime dt = Convert.ToDateTime("02-01-2019", culture);

    string result dt.ToString("dd/MMM/yyyy");

    result is 02-Jan-2019 

    Expected result is 02/Jan/2019 

    Saturday, March 9, 2019 1:19 PM
  • check this mate:

     CultureInfo MyCultureInfo = new CultureInfo("en-IN");

            string MyString = "02-01-2019";
            DateTime MyDateTime = DateTime.Parse(MyString, MyCultureInfo);
            Console.WriteLine(MyDateTime);


    Saturday, March 9, 2019 1:59 PM
  • I test your code and it was working with "/" as separator without any change.

    But you can try this:

    culture.DateTimeFormat.DateSeparator = "/";


    • Edited by DerChris88 Saturday, March 9, 2019 2:07 PM
    Saturday, March 9, 2019 2:06 PM
  • Your code output correctly for me.

    var culture = new CultureInfo("en-IN");
    var dt = Convert.ToDateTime("02-01-2019", culture);
    var result = dt.ToString("dd/MMM/yyyy");
    
    //result is 02/Jan/2019
    
    Console.WriteLine(result);


    Please remember to mark the replies as answers if they help and unmarked 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.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Saturday, March 9, 2019 2:59 PM
    Moderator
  • Hi

    Is your problem solved? If so, please post "Mark as answer" to the appropriate answer , so that it will help other members to find solution quickly if they faces similar issue.

    Best Regards,

    Jack


    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.

    Tuesday, March 12, 2019 6:20 AM
    Moderator
  • No,problem is still.

    Is it depend on iis setting or system regional setting..

    how can write code for conversion of date in any format which is independent on iis setting or system regional setting

    Wednesday, March 13, 2019 7:24 AM
  • Hi

    After checking it on windows server 2012 r2, I still could not reproduce your problem. My result is '02/Jan/2019'. You could refer to the following picture.

    Best Regards,

    Jack


    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.

    Wednesday, March 13, 2019 8:30 AM
    Moderator
  • i also tried with console application it gives correct result but in web app it not gives proper result.

    Is it depend on iis setting or system regional setting..

    Wednesday, March 13, 2019 8:34 AM
  • The date separator is culture-dependant - in your current culture it is defined as -. So, put the parts that you want to be output 'as is' inside quotes, as proposed here.
    Console.WriteLine("Date: " + datetime.ToString("M'/'d'/'yyyy"));
    or
    Console.WriteLine("Date: " + datetime.ToString("M\"/\"d\"/\"yyyy"));
    wizend
    Wednesday, March 13, 2019 12:23 PM
  • Hi

    If you have any question in a web app, you could post in the following forum.

    https://forums.asp.net/

    The Visual C# forum discusses and asks questions about the C# programming language, IDE, libraries, samples, and tools.<o:p></o:p>

    Best regards,                                                              

    Jack



    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.





    Thursday, March 14, 2019 7:05 AM
    Moderator
  • Thanks
    Thursday, March 14, 2019 10:42 AM