【问题标题】:Cross Country/Region DateTime Formating跨国家/地区日期时间格式
【发布时间】:2016-08-09 18:26:28
【问题描述】:

我正在为一个 ssis 包编写一个 C# 脚本,并在英国服务器上运行它,但我想以 yyyMMdd 的形式获得美国格式的日期时间。

但是当我执行 datevariablename.ToString("yyyyMMdd") 时,它会返回一个格式为 yyyyddMM 的字符串。我什至尝试使用 DateTime 的 Month、Day 和 Year 方法,但它仍然给了我日期作为月份和月份作为日期。

有谁知道我如何获得以 yyyyMMdd 格式返回的字符串?以下是我的代码的 sn-p:

private static readonly string[] dateFormat = {  "M/d/yyyy h:mm:ss tt", "M/d/yyyy h:mm tt", 
                                                     "MM/dd/yyyy hh:mm:ss", "M/d/yyyy h:mm:ss", 
                                                     "M/d/yyyy hh:mm tt", "M/d/yyyy hh tt", 
                                                     "M/d/yyyy h:mm", "M/d/yyyy h:mm", 
                                                     "MM/dd/yyyy hh:mm", "M/dd/yyyy hh:mm",
                                                     "d/M/yyyy h:mm:ss tt", "d/M/yyyy h:mm tt", 
                                                     "dd/MM/yyyy hh:mm:ss", "d/M/yyyy h:mm:ss", 
                                                     "d/M/yyyy hh:mm tt", "d/M/yyyy hh tt", 
                                                     "d/M/yyyy h:mm", "d/M/yyyy h:mm", 
                                                     "dd/MM/yyyy hh:mm", "dd/M/yyyy hh:mm"           
                                                  };

    public void Main()
    {
        // TODO: Add your code here
        Dts.TaskResult = (int)ScriptResults.Success;

        var date = DateTime.Now;
        var date2 = DateTime.Now;

        var inputDate = Dts.Variables[dateUnformated].Value;

        if (DateTime.TryParseExact(inputDate.ToString(), dateFormat, System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, out date))
        {
            var yyyymmdd = date.Year.ToString() + date.Month.ToString("0#") + date.Day.ToString("0#");

            Dts.Variables[Date1].Value = yyyymmdd;

            Dts.Variables[Date2].Value = date.ToString("yyyyMMdd");
        }

【问题讨论】:

  • 使用DateTime.ParseExact
  • 像这样向它提供 M/d 和 d/M 格式的混合列表意味着它将匹配它找到的第一个,这很容易在每个月的 12 天中出错
  • @Plutonix 好点,只要列表按所需优先级排序,我仍然认为它是有效的。因为最终没有人能告诉你 2016 年 1 月 1 日与 2016 年 1 月 1 日(MM/dd/yyyy vs dd/MM/yyyy)当月份和日期相同时:)
  • 如果存在现有日期 - inputDate - 则无需将其转换为字符串并将其解析回日期以“更改格式”。使用这种热门的格式这样做至少会偶尔更改日期。给定一个有效的日期,ValidDate.ToString("yyyyMMdd") 将产生相同的结果,无论文化如何。

标签: c# datetime ssis


【解决方案1】:

你试过这个吗?

DateTime dt = DateTime.ParseExact(dateString, "ddMMyyyy", CultureInfo.InvariantCulture);

dt.ToString("yyyyMMdd");

【讨论】:

  • 完美,我唯一需要改变的是我将日期格式设置为“dd/MM/yyyy hh:mm:ss”。当我运行时,您使用的“ddMMyyyy”失败了。否则它会起作用。
【解决方案2】:

所以还有一件事需要考虑。如果您在 SSMS 或 Excel 中查看日期或 .... 它将使用您计算机的文化,因为 datetime 是一个对象而不是字符串,并且您的计算机设置会告诉智能程序如何显示它。

所以如果 Date1 变量数据类型是日期或日期时间数据类型,您仍然会得到错误的格式。您可以将 Date1 var 更改为字符串以查看结果,或者您可以将结果转储到文本文件并在记事本或其他东西中打开。

乍一看,@Ozan 似乎为您提供了更有效地设置格式的好答案。

【讨论】:

    【解决方案3】:

    尝试使用DateTime.ParseExact方法实现See the Demo

    string strDate = "09-Aug-2016";
    DateTime datDate;
    if (DateTime.TryParseExact(strDate, new string[]
    {
    "dd-MMM-yyyy"
    }
    
    , System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, out datDate))
    {
        Console.WriteLine(datDate.ToString("yyyy/MM/dd"));
    }
    else
    {
        //Error in datetime format
    }
    

    甚至更简单的See the Demo

    DateTime datetime = DateTime.Now;
    Console.WriteLine(datetime.ToString("yyyy/MM/dd"));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-02-08
      • 2021-12-29
      • 2018-10-13
      • 1970-01-01
      • 1970-01-01
      • 2013-06-16
      相关资源
      最近更新 更多