<legend id='NCRFu'><style id='NCRFu'><dir id='NCRFu'><q id='NCRFu'></q></dir></style></legend>
        <bdo id='NCRFu'></bdo><ul id='NCRFu'></ul>

      <small id='NCRFu'></small><noframes id='NCRFu'>

      <i id='NCRFu'><tr id='NCRFu'><dt id='NCRFu'><q id='NCRFu'><span id='NCRFu'><b id='NCRFu'><form id='NCRFu'><ins id='NCRFu'></ins><ul id='NCRFu'></ul><sub id='NCRFu'></sub></form><legend id='NCRFu'></legend><bdo id='NCRFu'><pre id='NCRFu'><center id='NCRFu'></center></pre></bdo></b><th id='NCRFu'></th></span></q></dt></tr></i><div id='NCRFu'><tfoot id='NCRFu'></tfoot><dl id='NCRFu'><fieldset id='NCRFu'></fieldset></dl></div>

        <tfoot id='NCRFu'></tfoot>

        用年份格式化 TimeSpan

        Format A TimeSpan With Years(用年份格式化 TimeSpan)
        <tfoot id='EKzfM'></tfoot>

                <tbody id='EKzfM'></tbody>
                <bdo id='EKzfM'></bdo><ul id='EKzfM'></ul>
              • <i id='EKzfM'><tr id='EKzfM'><dt id='EKzfM'><q id='EKzfM'><span id='EKzfM'><b id='EKzfM'><form id='EKzfM'><ins id='EKzfM'></ins><ul id='EKzfM'></ul><sub id='EKzfM'></sub></form><legend id='EKzfM'></legend><bdo id='EKzfM'><pre id='EKzfM'><center id='EKzfM'></center></pre></bdo></b><th id='EKzfM'></th></span></q></dt></tr></i><div id='EKzfM'><tfoot id='EKzfM'></tfoot><dl id='EKzfM'><fieldset id='EKzfM'></fieldset></dl></div>

                <small id='EKzfM'></small><noframes id='EKzfM'>

                  <legend id='EKzfM'><style id='EKzfM'><dir id='EKzfM'><q id='EKzfM'></q></dir></style></legend>
                  本文介绍了用年份格式化 TimeSpan的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我有一个具有 2 个日期属性的类:FirstDayLastDay.LastDay 可以为空.我想生成 "x year(s) y day(s)" 格式的字符串.如果总年份小于 1,我想省略年份部分.如果总天数小于 1,我想省略天部分.如果年或日为 0,则应分别表示日/年",而不是日/年".

                  I have a class with 2 date properties: FirstDay and LastDay. LastDay is nullable. I would like to generate a string in the format of "x year(s) y day(s)". If the total years are less than 1, I would like to omit the year section. If the total days are less than 1, I would like to omit the day section. If either years or days are 0, they should say "day/year", rather than "days/years" respectively.

                  示例:
                  2.2年:             2年73天"
                  1.002738年:   "1年1天"
                  0.2年:             73天"
                  2年:                2年"

                  Examples:
                  2.2 years:             "2 years 73 days"
                  1.002738 years:   "1 year 1 day"
                  0.2 years:             "73 days"
                  2 years:                "2 years"

                  我有什么作品,但是很长:

                  What I have works, but it is long:

                  private const decimal DaysInAYear = 365.242M;
                  
                  public string LengthInYearsAndDays
                  {
                      get
                      {
                          var lastDay = this.LastDay ?? DateTime.Today;
                          var lengthValue = lastDay - this.FirstDay;
                  
                          var builder = new StringBuilder();
                  
                          var totalDays = (decimal)lengthValue.TotalDays;
                          var totalYears = totalDays / DaysInAYear;
                          var years = (int)Math.Floor(totalYears);
                  
                          totalDays -= (years * DaysInAYear);
                          var days = (int)Math.Floor(totalDays);
                  
                          Func<int, string> sIfPlural = value =>
                              value > 1 ? "s" : string.Empty;
                  
                          if (years > 0)
                          {
                              builder.AppendFormat(
                                  CultureInfo.InvariantCulture,
                                  "{0} year{1}",
                                  years,
                                  sIfPlural(years));
                  
                              if (days > 0)
                              {
                                  builder.Append(" ");
                              }
                          }
                  
                          if (days > 0)
                          {
                              builder.AppendFormat(
                                  CultureInfo.InvariantCulture,
                                  "{0} day{1}",
                                  days,
                                  sIfPlural(days));
                          }
                  
                          var length = builder.ToString();
                          return length;
                      }
                  }
                  

                  有没有更简洁的方法来做到这一点(但仍然可读)?

                  Is there a more concise way of doing this (but still readable)?

                  推荐答案

                  TimeSpan 没有年"的合理概念,因为它取决于起点和终点.(月份类似 - 29 天有多少个月?嗯,这取决于...)

                  A TimeSpan doesn't have a sensible concept of "years" because it depends on the start and end point. (Months is similar - how many months are there in 29 days? Well, it depends...)

                  为了给一个无耻的插件,我的 Noda Time 项目使这非常简单:

                  To give a shameless plug, my Noda Time project makes this really simple though:

                  using System;
                  using NodaTime;
                  
                  public class Test
                  {
                      static void Main(string[] args)
                      {
                          LocalDate start = new LocalDate(2010, 6, 19);
                          LocalDate end = new LocalDate(2013, 4, 11);
                          Period period = Period.Between(start, end,
                                                         PeriodUnits.Years | PeriodUnits.Days);
                  
                          Console.WriteLine("Between {0} and {1} are {2} years and {3} days",
                                            start, end, period.Years, period.Days);
                      }
                  }
                  

                  输出:

                  Between 19 June 2010 and 11 April 2013 are 2 years and 296 days
                  

                  这篇关于用年份格式化 TimeSpan的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Force JsonConvert.SerializeXmlNode to serialize node value as an Integer or a Boolean(强制 JsonConvert.SerializeXmlNode 将节点值序列化为整数或布尔值)
                  Using JSON to Serialize/Deserialize TimeSpan(使用 JSON 序列化/反序列化 TimeSpan)
                  Could not determine JSON object type for type quot;Classquot;(无法确定类型“Class的 JSON 对象类型.)
                  How to deserialize a JSONP response (preferably with JsonTextReader and not a string)?(如何反序列化 JSONP 响应(最好使用 JsonTextReader 而不是字符串)?)
                  how to de-serialize JSON data in which Timestamp it-self contains fields?(如何反序列化时间戳本身包含字段的JSON数据?)
                  JSON.Net custom contract serialization and Collections(JSON.Net 自定义合约序列化和集合)

                • <i id='0dRoG'><tr id='0dRoG'><dt id='0dRoG'><q id='0dRoG'><span id='0dRoG'><b id='0dRoG'><form id='0dRoG'><ins id='0dRoG'></ins><ul id='0dRoG'></ul><sub id='0dRoG'></sub></form><legend id='0dRoG'></legend><bdo id='0dRoG'><pre id='0dRoG'><center id='0dRoG'></center></pre></bdo></b><th id='0dRoG'></th></span></q></dt></tr></i><div id='0dRoG'><tfoot id='0dRoG'></tfoot><dl id='0dRoG'><fieldset id='0dRoG'></fieldset></dl></div>

                      <tbody id='0dRoG'></tbody>

                    <small id='0dRoG'></small><noframes id='0dRoG'>

                            <bdo id='0dRoG'></bdo><ul id='0dRoG'></ul>
                          • <tfoot id='0dRoG'></tfoot>
                            <legend id='0dRoG'><style id='0dRoG'><dir id='0dRoG'><q id='0dRoG'></q></dir></style></legend>