DateTime

作者:追风剑情 发布于:2015-12-2 11:43 分类:C#

示例一


  1. using System;
  2. using System.Globalization;
  3.  
  4. namespace DateTimeTest
  5. {
  6. class Program
  7. {
  8. static void Main(string[] args)
  9. {
  10. string timeStr;
  11. string timeFormat = "yyyy-MM-dd HH:mm:ss";
  12.  
  13. //格式化输出DateTime对象
  14. timeStr = DateTime.Now.ToString(timeFormat);
  15. //字符串时间格式转DateTime
  16. DateTime dt = DateTime.ParseExact(timeStr, timeFormat, CultureInfo.CurrentCulture);
  17.  
  18. Console.WriteLine("timeStr: {0}", timeStr);
  19. Console.WriteLine("dt: {0}", dt.ToString(timeFormat));
  20.  
  21. Console.Read();
  22. }
  23. }
  24. }


运行效果
date.png

示例:白班/夜班判断

using System;
/// <summary>
/// 关于白班与夜班时间判断
/// </summary>
public sealed class DayTimeDuty
{
	// 是否在夜班时间段
	public static bool IsNightShift()
	{
		return !IsDayShift();
	}

	// 是否在白班时间段 (8:00-20:00)
	public static bool IsDayShift()
	{
		DateTime dt = DateTime.Now;
		return (dt.Hour >= 8 && dt.Hour <= 19);
	}

	public static bool IsDayShift(DateTime dt)
	{
		return (dt.Hour >= 8 && dt.Hour <= 19);
	}

	// 是否在同班时间段
	public static bool IsSameShift(DateTime dt)
	{
		DateTime cdt = DateTime.Now;
		TimeSpan span = cdt - dt;
		//相差12小时,肯定不在同一个班次
		if (Math.Abs(span.TotalHours) >= 12)
			return false;
		return IsDayShift() == IsDayShift(dt);
	}

	// 是否在同班时间段
	public static bool IsSameShift(DateTime dt1, DateTime dt2)
	{
		TimeSpan span = dt2 - dt1;
		//相差12小时,肯定不在同一个班次
		if (Math.Abs(span.TotalHours) >= 12)
			return false;
		return IsDayShift(dt1) == IsDayShift(dt2);
	}
}

标签: C#

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号