码迷,mamicode.com
首页 > Windows程序 > 详细

C# 时间格式转换

时间:2020-04-14 22:57:57      阅读:91      评论:0      收藏:0      [点我收藏+]

标签:sre   time   end   时间   字符   his   返回   date   结束   

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Elight.Infrastructure
{
public static class ExtDateTime
{
/// <summary>
/// 获取格式化字符串,不带时分秒。格式:"yyyy-MM-dd"
/// </summary>
/// <param name="dateTime">日期</param>
public static string ToDateString(this DateTime dateTime)
{
return dateTime.ToString("yyyy-MM-dd");
}

/// <summary>
/// 获取格式化字符串,不带时分秒。格式:"yyyy-MM-dd"
/// </summary>
/// <param name="dateTime">日期</param>
public static string ToDateString(this DateTime? dateTime)
{
if (dateTime == null)
{
return string.Empty;
}
return ToDateString(dateTime.Value);
}

/// <summary>
/// 获取格式化字符串,不带年月日,格式:"HH:mm:ss"
/// </summary>
/// <param name="dateTime">日期</param>
public static string ToTimeString(this DateTime dateTime)
{
return dateTime.ToString("HH:mm:ss");
}

/// <summary>
/// 获取格式化字符串,不带年月日,格式:"HH:mm:ss"
/// </summary>
/// <param name="dateTime">日期</param>
public static string ToTimeString(this DateTime? dateTime)
{
if (dateTime == null)
{
return string.Empty;
}
return ToTimeString(dateTime.Value);
}

/// <summary>
/// 获取格式化字符串,带毫秒,格式:"yyyy-MM-dd HH:mm:ss.fff"
/// </summary>
/// <param name="dateTime">日期</param>
public static string ToMillisecondString(this DateTime dateTime)
{
return dateTime.ToString("yyyy-MM-dd HH:mm:ss.fff");
}

/// <summary>
/// 获取格式化字符串,带毫秒,格式:"yyyy-MM-dd HH:mm:ss.fff"
/// </summary>
/// <param name="dateTime">日期</param>
public static string ToMillisecondString(this DateTime? dateTime)
{
if (dateTime == null)
{
return string.Empty;
}
return ToMillisecondString(dateTime.Value);
}

/// <summary>
/// 获取格式化字符串,不带时分秒,格式:"yyyy年MM月dd日"
/// </summary>
/// <param name="dateTime">日期</param>
public static string ToChineseDateString(this DateTime dateTime)
{
return string.Format("{0}年{1}月{2}日", dateTime.Year, dateTime.Month, dateTime.Day);
}

/// <summary>
/// 获取格式化字符串,带时分秒,格式:"yyyy年MM月dd日 HH时mm分"
/// </summary>
/// <param name="dateTime">日期</param>
/// <param name="isRemoveSecond">是否移除秒</param>
public static string ToChineseDateTimeString(this DateTime dateTime, bool isRemoveSecond = false)
{
StringBuilder result = new StringBuilder();
result.AppendFormat("{0}年{1}月{2}日", dateTime.Year, dateTime.Month, dateTime.Day);
result.AppendFormat(" {0}时{1}分", dateTime.Hour, dateTime.Minute);

if (isRemoveSecond == false)
{
result.AppendFormat("{0}秒", dateTime.Second);
}

return result.ToString();
}

/// <summary>
/// 获取格式化字符串,带时分秒,格式:"yyyy年MM月dd日 HH时mm分"
/// </summary>
/// <param name="dateTime">日期</param>
/// <param name="isRemoveSecond">是否移除秒</param>
public static string ToChineseDateTimeString(this DateTime? dateTime, bool isRemoveSecond = false)
{
if (dateTime == null)
{
return string.Empty;
}

return ToChineseDateTimeString(dateTime.Value);
}

/// <summary>
/// 返回指定日期起始时间。
/// </summary>
/// <param name="dateTime"></param>
/// <returns></returns>
public static DateTime StartDateTime(this DateTime dateTime)
{
if (dateTime == null)
{
throw new ArgumentNullException();
}

return new DateTime(dateTime.Year, dateTime.Month, dateTime.Day, 0, 0, 0);
}
/// <summary>
/// 返回指定日期结束时间。
/// </summary>
/// <param name="dateTime"></param>
/// <returns></returns>
public static DateTime EndDateTime(this DateTime dateTime)
{
if (dateTime == null)
{
throw new ArgumentNullException();
}

return new DateTime(dateTime.Year, dateTime.Month, dateTime.Day, 23, 59, 59);
}
}
}

C# 时间格式转换

标签:sre   time   end   时间   字符   his   返回   date   结束   

原文地址:https://www.cnblogs.com/wugh8726254/p/12701407.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!