Skip to content

Latest commit

 

History

History
49 lines (37 loc) · 1.35 KB

AN0009.md

File metadata and controls

49 lines (37 loc) · 1.35 KB

AN0009: Fix TimeSpan formatting

Property Value
Id AN0009
Category Formatting
Default Severity Warning

When formatting a TimeSpan using one the following is always invalid:

Year and month does not apply TimeSpan

  • y
  • Y
  • yy
  • yyy
  • yyyy
  • M "Did you mean mm (minutes)"
  • MM "Did you mean mm (minutes)"
  • MMM
  • MMMM

TimeSpan doesn't operator with 12 vs 24h clock, so this is invalid:

  • HH "Did you mean hh (hours)"

  • D "Did you mean dd (days)"

  • DD "Did you mean dd (days)"

Special cases:

  • d alone doesn't work, use %d or in a pattern @"d\:h"
  • h alone doesn't work, use %h or in a pattern @"h\:m"
  • m alone doesn't work, use %d or in a pattern @"m\:s"
  • s alone doesn't work, use %s or in a pattern @"s\:f"
  • f alone doesn't work, use %f or in a pattern @"f\:ff"
  • F alone doesn't work, use %F or in a pattern @"F\:ff"

Example

Code with Diagnostic

var time = new TimeSpan(0, 10, 5).ToString(@"M", CultureInfo.InvariantCulture); // AN0009 - Month not valid in TimeSpan formatting. Did you mean 'm' (minutes)?

Code with Fix

var time = new TimeSpan(0, 10, 5).ToString(@"m", CultureInfo.InvariantCulture);