Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Globalization;
- namespace System
- {
- public static class DateTimeExtensions
- {
- /// <summary>
- /// 指定した日付を "yyyyMMdd" 形式で返します。
- /// </summary>
- /// <param name="value"></param>
- /// <returns></returns>
- public static string ToDbDateString(this DateTime value) => value.ToString("yyyyMMdd");
- /// <summary>
- /// データベースの "yyyyMMdd" 形式の文字列を <see cref="DateTime"/> 型に変換します。
- /// </summary>
- /// <param name="value"></param>
- /// <returns></returns>
- public static DateTime? ToDateTime(this string value)
- => DateTime.TryParseExact(value, "yyyyMMdd", null, DateTimeStyles.None, out var result)
- ? (DateTime?)result : null;
- }
- }
Add Comment
Please, Sign In to add comment