简体   繁体   English

将日期格式从 dd/MM/yyyy 转换为 MM/dd/yyyy

[英]Converting Date format from dd/MM/yyyy to MM/dd/yyyy

In our web application, we are supporting the Spanish language.在我们的 Web 应用程序中,我们支持西班牙语。 While collecting information we allowing users to input date in dd/MM/yyyy format for Spanish version website and MM/dd/yyyy for English version of the website.在收集信息时,我们允许用户以 dd/MM/yyyy 格式输入西班牙版网站的日期和 MM/dd/yyyy 格式的网站英文版。

After collecting that information we are sending the whole object to microservices to process.在收集到这些信息后,我们将整个对象发送到微服务进行处理。 Here I'm trying to convert the dd/MM/yyyy to MM/dd/yyyy before sending out information form my web application.在这里,我试图在从我的 Web 应用程序发送信息之前将 dd/MM/yyyy 转换为 MM/dd/yyyy。

I'm having trouble converting dd/MM/yyyy to MM/dd/yyyy.我在将 dd/MM/yyyy 转换为 MM/dd/yyyy 时遇到问题。

steps I followed to convert format:我遵循的步骤转换格式:

User input: 26/02/2020用户输入:26/02/2020

string effDate = effectivePolicyDate.ToString("MM/dd/yyyy", CultureInfo.CreateSpecificCulture("en-US"));

effectivePolicyDate is user input. EffectivePolicyDate 是用户输入。 from the above code, I'm getting effDate = "02/26/2020"(Getting expected result in a string.)从上面的代码,我得到 effDate = "02/26/2020"(在字符串中得到预期的结果。)

I need to send date in DateTime format not in string, so tried to convert it into DateTime below.我需要以 DateTime 格式而不是字符串发送日期,因此尝试将其转换为下面的 DateTime。

var date = DateTime.ParseExact(effDate, "MM/dd/yyyy", CultureInfo.InstalledUICulture);

Here I'm trying to parse the date, on date I'm getting 26/02/2020(dd/MM/yyyy) but not MM/dd/yyyy.在这里,我试图解析日期,在我收到 26/02/2020(dd/MM/yyyy) 但不是 MM/dd/yyyy 的日期。

var date2 = DateTime.Parse(effDate);

Above line is throwing below exception.上面的行抛出异常。

DateTime.Parse("2/26/2021");
'DateTime.Parse("2/26/2021")' threw an exception of type 'System.FormatException'
    Data: {System.Collections.ListDictionaryInternal}
    HResult: -2146233033
    HelpLink: null
    InnerException: null
    Message: "String '2/26/2021' was not recognized as a valid DateTime."
    Source: "System.Private.CoreLib"
    StackTrace: "   at System.DateTimeParse.Parse(ReadOnlySpan`1 s, DateTimeFormatInfo dtfi, DateTimeStyles styles)\r\n   at System.DateTime.Parse(String s)"
    TargetSite: {System.DateTime Parse(System.ReadOnlySpan`1[System.Char], System.Globalization.DateTimeFormatInfo, System.Globalization.DateTimeStyles)}
DateTime.Parse("26/2/2021");

I did not find a way to convert date format from dd/MM/yyyy to MM/dd/yyyy, I sent the date as it is and other applications handling.我没有找到将日期格式从 dd/MM/yyyy 转换为 MM/dd/yyyy 的方法,我按原样发送了日期和其他应用程序处理。 I'm curious to know is there away to convert the format.我很想知道是否可以转换格式。 I'm successful to convert into a string but failing to convert string(MM/dd/yyyy) date to Date property.我成功转换为字符串,但未能将字符串(MM/dd/yyyy)日期转换为日期属性。

Your question is somewhat unclear, but it seems that var date2 = DateTime.Parse(effDate);你的问题有点不清楚,但似乎var date2 = DateTime.Parse(effDate); is the issue.是问题。 I added two examples using DateTime.Parse in the test below, I hope this will help.我在下面的测试中添加了两个使用DateTime.Parse例子,我希望这会有所帮助。

[Test]
public void DateTests()
{
    var expectedDate = new DateTime(2020, 2, 26);

    var date1 = DateTime.Parse("2/26/2020", new CultureInfo("en-US"));
    Assert.AreEqual(expectedDate, date1);

    var date2 = DateTime.Parse("26/2/2020", new CultureInfo("es-ES"));
    Assert.AreEqual(expectedDate, date2);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将日期格式从 DD/MM/YYYY 或 MM/DD/YYYY 更改为 YYYY-MM-DD? - How to change date format from DD/MM/YYYY or MM/DD/YYYY to YYYY-MM-DD? 在ASP.NET Mvc中将dd / MM / yyyy字符串格式日期转换为MM / dd / yyyy日期格式 - Converting dd/MM/yyyy string format date to MM/dd/yyyy date format in ASP.NET Mvc MM / dd / yyyy格式 - MM/dd/yyyy format Controller 将日期从 dd/MM/yyyy 更改为 MM/dd/yyyy - Controller changing date from dd/MM/yyyy to MM/dd/yyyy 将 dd/MM/yyyy 输入转换为日期格式的 yyyy-MM-dd 而不是 varchar/datetime - Converting dd/MM/yyyy input to yyyy-MM-dd in date format not varchar/datetime 在将日期转换为“ MM / dd / yyyy”格式后,结果进入“ MM-dd-yyyy” - After converting date in “MM/dd/yyyy” format result comes in to “MM-dd-yyyy” 将日期从mm / dd / yyyy转换为从excel读取到yyyy / mm / dd,然后再将其发送到数据库 - Converting date from mm/dd/yyyy which is read from excel to yyyy/mm/dd before sending it to database 将asp:textbox / textmode中的日期从yyyy-mm-dd转换为dd-mm-yyyy? - Converting the date in asp:textbox/textmode from yyyy-mm-dd to dd-mm-yyyy? 如何将日期格式从MM / dd / yyyy更改为dd-MM-yyyy? - How to Change the Date Format from MM/dd/yyyy to dd-MM-yyyy? 如何在组合框中将日期格式从mm / dd / yyyy更改为dd-mm-yyyy? - how to change the date format from mm/dd/yyyy to dd-mm-yyyy in combobox?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM