Skip to content

Commit bc33180

Browse files
committed
Fixed commas in date formats. Bump version. Closes #22
1 parent b70c420 commit bc33180

File tree

4 files changed

+13
-5
lines changed

4 files changed

+13
-5
lines changed

src/ExcelNumberFormat/ExcelNumberFormat.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<TargetFrameworks>net20;netstandard1.0;netstandard2.0</TargetFrameworks>
5-
<VersionPrefix>1.0.8</VersionPrefix>
5+
<VersionPrefix>1.0.9</VersionPrefix>
66
<GenerateDocumentationFile>true</GenerateDocumentationFile>
77
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
88
<Description>.NET library to parse ECMA-376 number format strings and format values like Excel and other spreadsheet softwares.</Description>

src/ExcelNumberFormat/Formatter.cs

+10-1
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ private static string FormatDate(DateTime date, List<string> tokens, CultureInfo
202202
else if (token.StartsWith("h", StringComparison.OrdinalIgnoreCase))
203203
{
204204
var digits = token.Length;
205-
if(containsAmPm)
205+
if (containsAmPm)
206206
result.Append((date.Hour % 12).ToString("D" + digits));
207207
else
208208
result.Append(date.Hour.ToString("D" + digits));
@@ -256,6 +256,15 @@ private static string FormatDate(DateTime date, List<string> tokens, CultureInfo
256256
result.Append(culture.DateTimeFormat.DateSeparator);
257257
#endif
258258
}
259+
else if (token == ",")
260+
{
261+
while (i < tokens.Count - 1 && tokens[i + 1] == ",")
262+
{
263+
i++;
264+
}
265+
266+
result.Append(",");
267+
}
259268
else
260269
{
261270
FormatLiteral(token, result);

src/ExcelNumberFormat/NumberFormat.cs

-3
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,6 @@ public NumberFormat(string formatString)
6262
/// <returns>The formatted string.</returns>
6363
public string Format(object value, CultureInfo culture)
6464
{
65-
if (!IsValid || string.IsNullOrEmpty(FormatString))
66-
CompatibleConvert.ToString(value, culture);
67-
6865
var section = Evaluator.GetSection(Sections, value);
6966
if (section == null)
7067
return CompatibleConvert.ToString(value, culture);

test/ExcelNumberFormat.Tests/Class1.cs

+2
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,8 @@ public void TestDate()
139139
Test(new DateTime(2020, 1, 1, 14, 35, 55), "m/d/yyyy\\ h:mm:ss;@", "1/1/2020 14:35:55");
140140
Test(new DateTime(2020, 1, 1, 14, 35, 55), "m/d/yyyy\\ hh:mm:ss AM/PM;@", "1/1/2020 02:35:55 PM");
141141
Test(new DateTime(2020, 1, 1, 16, 5, 6), "m/d/yyyy\\ h:m:s AM/PM;@", "1/1/2020 4:5:6 PM");
142+
Test(new DateTime(2017, 10, 16, 0, 0, 0), "dddd, MMMM d, yyyy", "Monday, October 16, 2017");
143+
Test(new DateTime(2017, 10, 16, 0, 0, 0), "dddd,,, MMMM d,, yyyy,,,,", "Monday, October 16, 2017,");
142144
}
143145

144146
[TestMethod]

0 commit comments

Comments
 (0)