【问题标题】:show past and future date asp.net c#显示过去和未来的日期asp.net c#
【发布时间】:2015-05-23 03:56:23
【问题描述】:

我需要显示如下日期。 (日期为西班牙语)

月 16 日 de marzo | Martes 17 de marzo | Miércoles 18 de marzo | Jueves 19 de marzo |维尔内斯 20 de marzo 20

我需要显示 5 个日期,组中的第三个日期必须是今天。

有人知道如何开始吗?

【问题讨论】:

  • DateTime.TodayDateTime.AddDays()

标签: c# asp.net datetime c#-4.0


【解决方案1】:

你可以使用这样的东西:

DateTime today = DateTime.Now;
DateTime tomorrow = today.AddDays(1);
DateTime yesterday = today.AddDays(-1);

然后你只需按照你需要的方式格式化你的输出。

【讨论】:

    【解决方案2】:

    在 C# 中,可以使用 DateTime 对象获取当前的 DateTime,然后使用 DateTime 的方法获取前 2 和后 2。DateTime 的 AddDay(或 AddMinute、Second 等)可以取负数。

    DateTime myDate = DateTime.Now;
    DateTime prevOne = myDate.AddDays(-1);
    DateTime prevTwo = myDate.AddDays(-2);
    DateTime nextOne = myDate.AddDays(1);
    DateTime nextTwo = myDate.AddDays(2);
    

    按 prevTwo、prevOne、myDate、nextOne、nextTwo 的顺序显示它们。我假设您的区域设置负责翻译成西班牙语。

    【讨论】:

      【解决方案3】:

      我会按照这些思路做某处:

      Thread.CurrentThread.CurrentCulture = new CultureInfo("es-ES");
      //Format your date 'de' to get the literal string into the date
      var datestring = "{0:dddd dd 'de' MMMM}";
      
      StringBuilder sb = new StringBuilder();
      //iterate
      for(int x = 0; x < 5; x++)
      {
         //build the string
          sb.Append(String.Format(datestring + " | ", DateTime.Now.AddDays(-2+x)));
      }
      sb.ToString().Dump();
      

      输出:

      martes 17 de marzo | miércoles 18 de marzo | jueves 19 de marzo | 维尔内斯20 de marzo | sábado 21 de marzo

      edit:一种更好的方法,它去掉了尾随的“|”并将数据聚合与演示分开:

      Thread.CurrentThread.CurrentCulture = new CultureInfo("es-ES");
      
      var dateFormat = "dddd dd 'de' MMMM";
      List<DateTime> dates = new List<DateTime>();
      for(int x = 0; x < 5; x++)
      {
          //push dates into our List
          dates.Add(DateTime.Now.AddDays(-2+x));
      }
      //build the output string and format our dates
      String.Join(" | ", dates.Select (d => d.ToString(dateFormat))).Dump();
      

      【讨论】:

        【解决方案4】:

        在这里,cmets inline:

        using System;
        using System.Linq;
        using System.Text;
        using System.Globalization;
        
        class Program
        {
            static void Main(string[] args)
            {
                // slecting locale
                var ci = new CultureInfo("es-ES");
        
                // use a StringBuilder for storing the processed text
                var sBuilder = new StringBuilder();
        
                // use an Enumerable
                Enumerable.Range(-2, 5)
                    // get date range
                    .Select(i => DateTime.Today.AddDays(i))
                    // get long dates in es-ES and remove ","
                    .Select(i => i.ToString("D", ci).Replace(",", ""))
                    .ToList().ForEach(s =>
                    {
                        // capitalize first letter
                        sBuilder.Append(char.ToUpper(s[0]));
                        // remove the year part
                        sBuilder.Append(s.Substring(1, s.LastIndexOf(' ') - 3));
                        // add delimiter
                        sBuilder.Append("| ");
                    });
                // adjust length for removing the final delimiter
                sBuilder.Length = sBuilder.Length - 2;
        
                Console.WriteLine(sBuilder.ToString());
            }
        }
        

        【讨论】:

          【解决方案5】:

          伪代码:

          Declare a string
          For (var i=0; i<5;i++)
          {      
            string += " (today -2 +i) formatted in any way you want "
          }
          Display the string
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2020-08-05
            • 1970-01-01
            • 2023-04-01
            • 2011-03-16
            • 2022-10-18
            • 1970-01-01
            • 1970-01-01
            • 2022-07-31
            相关资源
            最近更新 更多