Unity 使用this关键字进行函数拓展 - DateTime
【摘要】
Example:
using System;using UnityEngine; using SK.Framework; public class Foo : MonoBehaviour{ private void Start() { DateTime dt = DateTime.Now; /...
Example:
-
using System;
-
using UnityEngine;
-
-
using SK.Framework;
-
-
public class Foo : MonoBehaviour
-
{
-
private void Start()
-
{
-
DateTime dt = DateTime.Now;
-
-
//周六
-
dt.DayOfWeek.ToChinese();
-
//星期六
-
dt.DayOfWeek.ToChinese2();
-
-
//yyyy/MM/dd
-
dt.ToDateString();
-
//yyyy-MM-dd
-
dt.ToDateString2();
-
//yyyy年MM月dd日
-
dt.ToDateString3();
-
-
//yyyy/MM/dd HH:mm:ss
-
dt.ToDateTimeString();
-
//yyyy-MM-dd HH:mm:ss
-
dt.ToDateTimeString2();
-
//yyyy年MM月dd日 HH:mm:ss
-
dt.ToDateTimeString3();
-
}
-
}
Extension:
-
using System;
-
-
namespace SK.Framework
-
{
-
/// <summary>
-
/// DateTime日期时间类相关拓展
-
/// </summary>
-
public static class DateTimeExtension
-
{
-
/// <summary>
-
/// 获取时间戳
-
/// </summary>
-
/// <param name="self">时间</param>
-
/// <returns>时间戳</returns>
-
public static double GetTimeStamp(this DateTime self)
-
{
-
//计算机元年 1970年1月1日0时0分0秒
-
return (self - new DateTime(1970, 1, 1, 0, 0, 0, 0)).TotalSeconds;
-
}
-
/// <summary>
-
/// 转换为中文 (周*)
-
/// </summary>
-
/// <param name="self"></param>
-
/// <returns></returns>
-
public static string ToChinese(this DayOfWeek self)
-
{
-
switch (self)
-
{
-
case DayOfWeek.Monday: return "周一";
-
case DayOfWeek.Tuesday: return "周二";
-
case DayOfWeek.Wednesday: return "周三";
-
case DayOfWeek.Thursday: return "周四";
-
case DayOfWeek.Friday: return "周五";
-
case DayOfWeek.Saturday: return "周六";
-
case DayOfWeek.Sunday: return "周日";
-
default: return null;
-
}
-
}
-
/// <summary>
-
/// 转换为中文 (星期*)
-
/// </summary>
-
/// <param name="self"></param>
-
/// <returns></returns>
-
public static string ToChinese2(this DayOfWeek self)
-
{
-
switch (self)
-
{
-
case DayOfWeek.Monday: return "星期一";
-
case DayOfWeek.Tuesday: return "星期二";
-
case DayOfWeek.Wednesday: return "星期三";
-
case DayOfWeek.Thursday: return "星期四";
-
case DayOfWeek.Friday: return "星期五";
-
case DayOfWeek.Saturday: return "星期六";
-
case DayOfWeek.Sunday: return "星期日";
-
default: return null;
-
}
-
}
-
/// <summary>
-
/// 转换格式 yyyy/MM/dd
-
/// </summary>
-
/// <param name="self"></param>
-
/// <returns>yyyy/MM/dd</returns>
-
public static string ToDateString(this DateTime self)
-
{
-
return self.ToString("yyyy/MM/dd");
-
}
-
/// <summary>
-
/// 转换格式 yyyy-MM-dd
-
/// </summary>
-
/// <param name="self"></param>
-
/// <returns>yyyy-MM-dd</returns>
-
public static string ToDateString2(this DateTime self)
-
{
-
return self.ToString("yyyy-MM-dd");
-
}
-
/// <summary>
-
/// 转换格式 yyyy年MM月dd日
-
/// </summary>
-
/// <param name="self"></param>
-
/// <returns>yyyy年MM月dd日</returns>
-
public static string ToDateString3(this DateTime self)
-
{
-
return self.ToString("yyyy年MM月dd日");
-
}
-
/// <summary>
-
/// 转换格式 yyyy/MM/dd HH:mm:ss
-
/// </summary>
-
/// <param name="self"></param>
-
/// <returns>yyyy/MM/dd HH:mm:ss</returns>
-
public static string ToDateTimeString(this DateTime self)
-
{
-
return self.ToString("yyyy/MM/dd HH:mm:ss");
-
}
-
/// <summary>
-
/// 转换格式 yyyy-MM-dd HH:mm:ss
-
/// </summary>
-
/// <param name="self"></param>
-
/// <returns>yyyy-MM-dd HH:mm:ss</returns>
-
public static string ToDateTimeString2(this DateTime self)
-
{
-
return self.ToString("yyyy-MM-dd HH:mm:ss");
-
}
-
/// <summary>
-
/// 转换格式 yyyy年MM月dd日 HH:mm:ss
-
/// </summary>
-
/// <param name="self"></param>
-
/// <returns>yyyy年MM月dd日 HH:mm:ss</returns>
-
public static string ToDateTimeString3(this DateTime self)
-
{
-
return self.ToString("yyyy年MM月dd日 HH:mm:ss");
-
}
-
}
-
}
文章来源: coderz.blog.csdn.net,作者:CoderZ1010,版权归原作者所有,如需转载,请联系作者。
原文链接:coderz.blog.csdn.net/article/details/120470141
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)