Skip to content

Commit

Permalink
Working towards fixing date formatting RicoSuter#520
Browse files Browse the repository at this point in the history
  • Loading branch information
danzel committed Oct 21, 2017
1 parent 5eda757 commit 5f4e1c3
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public async Task When_date_handling_is_moment_then_moment_property_are_generate
//// Assert
Assert.IsTrue(code.Contains("myDate: moment.Moment"));
Assert.IsTrue(code.Contains("this.myDate = data[\"myDate\"] ? moment(data[\"myDate\"].toString()) : <any>undefined;"));
Assert.IsTrue(code.Contains("data[\"myDate\"] = this.myDate ? this.myDate.toISOString().slice(0, 10) : <any>undefined;"));
Assert.IsTrue(code.Contains("data[\"myDate\"] = this.myDate ? this.myDate.format('YYYY-MM-DD') : <any>undefined;"));
}

[TestMethod]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ private static object CreateModel(DataConversionParameters parameters)

//StringToDateCode is used for date and date-time formats
StringToDateCode = parameters.Settings.DateTimeType == TypeScriptDateTimeType.Date ? "new Date" : "moment",
DateToStringCode = "toISOString().slice(0, 10)",
DateToStringCode = parameters.Settings.DateTimeType == TypeScriptDateTimeType.Date ? "formatDate(THIS)" : "THIS.format('YYYY-MM-DD')",
DateTimeToStringCode = "toISOString()",

HandleReferences = parameters.Settings.HandleReferences
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ public FileTemplateModel(TypeScriptGeneratorSettings settings)
/// <summary>Gets a value indicating whether to handle JSON references.</summary>
public bool HandleReferences => _settings.HandleReferences;

/// <summary>Gets a value indicating whether Date(Time) objects are handled using JS Dates</summary>
public bool UsesJsDate => _settings.DateTimeType == TypeScriptDateTimeType.Date;

/// <summary>Gets the reference handling code.</summary>
public string ReferenceHandlingCode => TypeScriptReferenceHandlingCodeGenerator.Generate(); // TODO: Remove after T4 has been removed
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ if (<#=Model.Value#> && <#=Model.Value#>.constructor === Array) {
<#=Model.Variable#>.push(item.toJSON());
<#}else{#>
<#if(Model.IsArrayItemDate){#>
<#=Model.Variable#>.push(item.<#=Model.DateToStringCode#>);
<#=Model.Variable#>.push(<#=Model.DateToStringCode.Replace("THIS", "item")#>);
<#}else if(Model.IsArrayItemDateTime){#>
<#=Model.Variable#>.push(item.<#=Model.DateTimeToStringCode#>);
<#}else{#>
Expand All @@ -27,7 +27,7 @@ if (<#=Model.Value#>) {
<#=Model.Variable#>[key] = <#=Model.Value#>[key] ? <#=Model.Value#>[key].toJSON() : <any><#=Model.NullValue#>;
<#}else{#>
<#if(Model.IsDictionaryValueDate){#>
<#=Model.Variable#>[key] = <#=Model.Value#>[key] ? <#=Model.Value#>[key].<#=Model.DateToStringCode#> : <any><#=Model.NullValue#>;
<#=Model.Variable#>[key] = <#=Model.Value#>[key] ? <#=Model.DateToStringCode.Replace("THIS", Model.Value + "[key]")#> : <any><#=Model.NullValue#>;
<#} else if(Model.IsDictionaryValueDateTime){#>
<#=Model.Variable#>[key] = <#=Model.Value#>[key] ? <#=Model.Value#>[key].<#=Model.DateTimeToStringCode#> : <any><#=Model.NullValue#>;
<#}else{#>
Expand All @@ -41,7 +41,7 @@ if (<#=Model.Value#>) {
}
<#}else{#>
<# if(Model.IsDate){#>
<#=Model.Variable#> = <#=Model.Value#> ? <#=Model.Value#>.<#=Model.DateToStringCode#> : <#if(Model.HasDefaultValue){#><#=Model.DefaultValue#><#}else{#><any><#=Model.NullValue#><#}#>;
<#=Model.Variable#> = <#=Model.Value#> ? <#=Model.DateToStringCode.Replace("THIS", Model.Value)#> : <#if(Model.HasDefaultValue){#><#=Model.DefaultValue#><#}else{#><any><#=Model.NullValue#><#}#>;
<# } else if(Model.IsDateTime){#>
<#=Model.Variable#> = <#=Model.Value#> ? <#=Model.Value#>.<#=Model.DateTimeToStringCode#> : <#if(Model.HasDefaultValue){#><#=Model.DefaultValue#><#}else{#><any><#=Model.NullValue#><#}#>;
<# }else{#>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@

<#=Model.ExtensionCode.ImportCode#>

<#if(Model.UsesJsDate){#>
function formatDate(d: Date) {
return d.getFullYear() + '-' + (d.getMonth() < 9 ? ('0' + (d.getMonth()+1)) : (d.getMonth()+1)) + '-' + (d.getDate() < 10 ? ('0' + d.getDate()) : d.getDate());
}
<#}#>

<#if(Model.HasModuleName){#>
module <#=Model.ModuleName#> {
<#}
Expand Down

0 comments on commit 5f4e1c3

Please sign in to comment.