-
-
Notifications
You must be signed in to change notification settings - Fork 486
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #349 from YaChengMu/master
日期格式化支持DateTimeOffset类型
- Loading branch information
Showing
5 changed files
with
91 additions
and
2 deletions.
There are no files selected for viewing
37 changes: 37 additions & 0 deletions
37
src/Magicodes.ExporterAndImporter.Core/Extension/ObjectExtension.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
namespace Magicodes.ExporterAndImporter.Core.Extension | ||
{ | ||
/// <summary> | ||
/// | ||
/// </summary> | ||
public static class ObjectExtension | ||
{ | ||
/// <summary> | ||
/// 将 DateTimeOffset 转换成本地 DateTime | ||
/// </summary> | ||
/// <param name="dateTime"></param> | ||
/// <returns></returns> | ||
public static DateTime ConvertToDateTime(this DateTimeOffset dateTime) | ||
{ | ||
if (dateTime.Offset.Equals(TimeSpan.Zero)) | ||
return dateTime.UtcDateTime; | ||
if (dateTime.Offset.Equals(TimeZoneInfo.Local.GetUtcOffset(dateTime.DateTime))) | ||
return dateTime.ToLocalTime().DateTime; | ||
else | ||
return dateTime.DateTime; | ||
} | ||
|
||
/// <summary> | ||
/// 将 DateTimeOffset? 转换成本地 DateTime? | ||
/// </summary> | ||
/// <param name="dateTime"></param> | ||
/// <returns></returns> | ||
public static DateTime? ConvertToDateTime(this DateTimeOffset? dateTime) | ||
{ | ||
return dateTime.HasValue ? (DateTime?)dateTime.Value.ConvertToDateTime() : null; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters