C# 之 DataTime常用方法
【摘要】 C# DataTime
参数format格式详细用法:
格式字符关联属性/说明dShortDatePatternDLongDatePatternf完整日期和时间(长日期和短时间)FFullDateTimePattern(长日期和长时间)g常规(短日期和短时间)G常规(短日期和长时间)mMMonthDayPatternrRRFC1123Patterns使用当地时间的S...
C# DataTime
参数format格式详细用法:
格式字符 | 关联属性/说明 |
---|---|
d | ShortDatePattern |
D | LongDatePattern |
f | 完整日期和时间(长日期和短时间) |
F | FullDateTimePattern(长日期和长时间) |
g | 常规(短日期和短时间) |
G | 常规(短日期和长时间) |
m | MMonthDayPattern |
r | RRFC1123Pattern |
s | 使用当地时间的SortableDateTimePattern(基于ISO8601) |
t | ShortTimePattern |
T | LongTimePattern |
u | UniversalSortableDateTimePattern用于显示通用时间的格式 |
U | 使用通用时间的完整日期和时间(长日期和长时间) |
y | YYearMonthPattern |
当前时间个获取:
namespace DataTimeTest
{ class Program { static void Main(string[] args) { //HH:24小时 hh:12小时 Console.WriteLine("yyyy-MM-dd HH:mm:ss:fff " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff")); Console.WriteLine("yyyy-MM-dd HH:mm:ss:ms " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ms")); Console.WriteLine("y " + DateTime.Now.ToString("y")); Console.WriteLine("dddd "+DateTime.Now.ToString("dddd")); Console.WriteLine("dddd, MMMM dd yyyy " + DateTime.Now.ToString("dddd, MMMM dd yyyy")); Console.WriteLine("ddd, MMM d yy " + DateTime.Now.ToString("ddd, MMM d yy")); Console.WriteLine("dddd, MMMM dd " + DateTime.Now.ToString("dddd, MMMM dd")); Console.WriteLine("M/yy " + DateTime.Now.ToString("M/yy")); Console.WriteLine("dd-MM-yy " + DateTime.Now.ToString("dd-MM-yy")); Console.WriteLine(); Console.WriteLine("ToString('t'): "+DateTime.Now.ToString("t")); Console.WriteLine("ToString('d'): "+DateTime.Now.ToString("d")); Console.WriteLine("ToString('D'): "+DateTime.Now.ToString("D")); Console.WriteLine("ToString('f'): "+DateTime.Now.ToString("f")); Console.WriteLine("ToString('F'): "+DateTime.Now.ToString("F")); Console.WriteLine("ToString('G'): "+DateTime.Now.ToString("G")); Console.WriteLine("ToString('t'): "+DateTime.Now.ToString("t")); Console.WriteLine("ToString('T'): "+DateTime.Now.ToString("T")); Console.WriteLine("ToString('U'): "+DateTime.Now.ToString("U")); Console.WriteLine("ToString('u'): "+DateTime.Now.ToString("u")); Console.WriteLine("ToString('m'): "+DateTime.Now.ToString("m")); Console.WriteLine("ToString('M'): "+DateTime.Now.ToString("M")); Console.WriteLine("ToString('r'): "+DateTime.Now.ToString("r")); Console.WriteLine("ToString('R'): "+DateTime.Now.ToString("R")); Console.WriteLine("ToString('y'): "+DateTime.Now.ToString("y")); Console.WriteLine("ToString('Y'): "+DateTime.Now.ToString("Y")); Console.WriteLine("ToString('o'): "+DateTime.Now.ToString("o")); Console.WriteLine("ToString('O'): "+DateTime.Now.ToString("O")); Console.WriteLine("ToString('s'): "+DateTime.Now.ToString("s")); Console.ReadKey(); } }
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
实用小方法
计算时间差
//调用示例: DateDiff(Convert.ToDateTime("2019/03/30 11:13:10"), Convert.ToDateTime("2019/04/01 12:24:11")); DateDiff(Convert.ToDateTime("2019-03-30 11:13:10"), Convert.ToDateTime("2019-04-01 12:24:11")); /// <summary> /// 计算两个日期的时间间隔 /// </summary> /// <param name="DateTime1">第一个日期和时间</param> /// <param name="DateTime2">第二个日期和时间</param> /// <returns>返回间隔几天</returns> private static int DateDiff(DateTime DateTime1, DateTime DateTime2) { string dateDiff = null; TimeSpan ts1 = new TimeSpan(DateTime1.Ticks); TimeSpan ts2 = new TimeSpan(DateTime2.Ticks); TimeSpan ts = ts1.Subtract(ts2).Duration(); dateDiff = ts.Days.ToString() + "天" + ts.Hours.ToString() + "小时" + ts.Minutes.ToString() + "分钟" + ts.Seconds.ToString() + "秒"; Console.WriteLine(dateDiff); //只返回需要的时间间隔 return ts.Days; }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
C# DateTime转换为Unix时间戳
System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)); // 当地时区
long timeStamp = (long)(DateTime.Now - startTime).TotalSeconds; // 相差秒数
System.Console.WriteLine(timeStamp);
- 1
- 2
- 3
Unix时间戳转换为C# DateTime
long unixTimeStamp = 1478162177;
System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)); // 当地时区
DateTime dt = startTime.AddSeconds(unixTimeStamp);
System.Console.WriteLine(dt.ToString("yyyy/MM/dd HH:mm:ss:ffff"));
- 1
- 2
- 3
- 4
文章来源: czhenya.blog.csdn.net,作者:陈言必行,版权归原作者所有,如需转载,请联系作者。
原文链接:czhenya.blog.csdn.net/article/details/88125254
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)