DateTime函数
一、初始化:
DateTime dt = , , ); DateTime dt1 = DateTime.Now; DateTime dt2 = DateTime.Today; DateTime dt3 = , , , , , ); DateTime dt4 = , , , new System.Globalization.ChineseLunisolarCalendar());//农历日期 DateTime dt5 = , DateTimeKind.Local);
二、格式化函数。
1、常用
DateTime dt = DateTime.Now;
s1=dt.ToString();//2019-06-25 10:38:31
s2=dt.ToString("d");//2019-06-25
s3=dt.toToLocalTime().ToString();//2019-06-25 10:38:31
s4=dt.ToUniversalTime().ToString();//2019-06-25 2:38:31
2、长短日期和时间
s1 = dt.ToLongDateString();//2019年6月25日 s2 = dt.ToShortDateString();//2019-06-25 s3 = dt.ToLongTimeString();//10:41:14 s4 = dt.ToShortTimeString();//10:41
三、属性
1、日期部分
s1 = dt.Date.ToString();//日期。2019-06-25 0:00:00 s2 = dt.Year.ToString();//年,2019 s3 = dt.Month.ToString();//月、6 s4 = dt.Day.ToString();//日 25 s5 = dt.DayOfWeek.ToString();//星期.Tuesday s6=Convert.ToInt32( dt.DayOfWeek).ToString(); s7 = dt.DayOfYear.ToString();// 该年中的第几天。176 s8 = (dt.DayOfYear / + ).ToString();//该年中的第几周。26
2、时间部分
s1 = dt.TimeOfDay.ToString();//时间。10:50:23.4881958 s2 = dt.Hour.ToString();//时,10 s3 = dt.Minute.ToString();//分、50 s4 = dt.Second.ToString();//秒 23 s5 = dt.Millisecond.ToString();).ToString();// 2020-06-25 11:01:09 s2 = dt.AddMonths(-).ToString();// 2019-05-25 11:01:24 s3 = dt.AddDays().ToString();// 2019-06-26 11:01:24 s4 = dt.AddHours(-).ToString();// 2019-06-25 10:01:24 s5 = dt.AddMinutes().ToString();//2019-06-25 11:02:24 s6 = dt.AddTicks().ToString();//2019-06-25 11:01:24
日期加减TimeSpan:
DateTime dt = DateTime.Now; s1 = dt + TimeSpan.FromDays(2.1).ToString();//2019-06-25 11:05:392.02:24:00 s2 = dt.Add(TimeSpan.FromDays(2.1)).ToString();//2019-06-27 13:29:39 s3 = (dt - , , , , )).ToString();// 2019-06-24 9:05:39 s4 = dt.Subtract(TimeSpan.FromDays()).ToString();)).Days.ToString(); s2 = (dt.Subtract(dt.AddDays(-))).Days.ToString();
3、比较时间
s1 = dt.CompareTo(dt.AddYears()).ToString();//-1
s2 = (dt == dt.AddYears()).ToString();//False
s3 = (dt < dt.AddYears()).ToString();, ).ToString();//某月共有几天,30
s2 = DateTime.IsLeapYear().ToString();//是否为闰年,False
s3 = (DateTime.Compare(dt, , , ))).ToString();//比较. 1
s4 = DateTime.Parse("2019,5,16").ToString();//2019-05-16 0:00:00
s5 = DateTime.ParseExact("1899-12-30", "yyyy-MM-dd", CultureInfo.CurrentCulture.DateTimeFormat).ToString();//1899-12-30 0:00:00
s6 = Convert.ToDateTime("2019,5,16").ToString();//2019-05-16 0:00:00
六、计算一年中的第几周
1、见上面的dt.DayOfYear。
2、利用GregorianCalendar
GregorianCalendar gc = new GregorianCalendar(); int weekOfYear = gc.GetWeekOfYear(DateTime.Now, CalendarWeekRule.FirstDay, DayOfWeek.Monday); s1 = weekOfYear.ToString();
DateTime函数的更多相关文章
- datetime函数和random.seed()函数的应用
一,datetime 在python中datetime是一个库是一个模块也是一个函数,作用很多,这里面只对其做简单的最常用的讲解. 首先返回系统时间 import datetime nowTime=d ...
- php strtotime,mktime,DateTime函数处理时间累加问题
时间戳(年月日时分秒) 使用strtotime函数,结合+1 month,-1 month,next month,last month的时候会出现一些问题. demo示例: //时间"20 ...
- python 获取年月日时分秒 获取当前时间 datetime函数
import datetime#取当前时间print(datetime.datetime.now())#取年print(datetime.datetime.now().year)#取月print(da ...
- js DateTime函数
---恢复内容开始--- 一.js获取当前日期时间var myDate = new Date();myDate.getYear(); //获取当前年份(2位)myDate.getFull ...
- Sql Server函数全解(四)日期和时间函数
日期和时间函数主要用来处理日期和时间值,本篇主要介绍各种日期和时间函数的功能和用法,一般的日期函数除了使用date类型的参数外,也可以使用datetime类型的参数,但会忽略这些值的时间部分.相同 ...
- PB函数大全
PB函数大全 Abs()功能计算绝对值.语法Abs ( n )参数n:要得到绝对值的数值型变量或表达式返回值返回值的数据类型与n的数据类型相同,函数执行成功时返回n的绝对值.如果参数n的值为NULL, ...
- SQLite中的时间日期函数(转)
SQLite包含了如下时间/日期函数: datetime().......................产生日期和时间date()...........................产生日期tim ...
- Delphi常用系统函数总结
Delphi常用系统函数总结 字符串处理函数 Unit System 函数原型 function Concat(s1 [, s2,..., sn]: string): string; 说明 与 S : ...
- SQL Server编程(02)自定义函数
在编程过程中,我们通常把特定的功能语句块封装称函数,方便代码的重用.我们可以在SQL Server中自定义函数,根据函数返回值的区别,我们自定义的函数分两种:标量值函数和表值函数. 自定义函数的优点: ...
随机推荐
- Appium移动自动化测试-----(七)Desired Capabilities
Desired Capabilities Desired Capabilities 在启动 session 的时候是必须提供的. Desired Capabilities 本质上是以 key valu ...
- linux 给运行程序指定动态库路径
1. 连接和运行时库文件搜索路径到设置 库文件在连接(静态库和共享 库)和运行(仅限于使用共享库的程序)时被使用,其搜索路径是在系统中进行设置的.一般 Linux 系统把 /lib 和 /usr/li ...
- 032 Android智能下拉刷新框架-SmartRefreshLayout+RecyclerView的使用
1.SmartRefreshLayout介绍 SmartRefreshLayout的目标是打造一个强大,稳定,成熟的下拉刷新框架,并集成各种的炫酷.多样.实用.美观的Header和Footer. 正如 ...
- Guava 工具类之Cache的使用
一.guava cache 介绍 1.介绍 guava cache是Google guava中提供的一款轻量级的本地缓存组件,其特点是简单.轻便.完善.扩展性强,内存管理机制也相对完善. 2.使用缓存 ...
- [转帖]鲁大师Q3季度PC处理器排行:AMD、Intel终于五五开了
鲁大师Q3季度PC处理器排行:AMD.Intel终于五五开了 https://www.cnbeta.com/articles/tech/902375.htm 近日,鲁大师发布了Q3季度PC处理器排行. ...
- [转帖]QC 和 PD:关于你所不知道的快充
QC 和 PD:关于你所不知道的快充 http://www.sohu.com/a/276214250_465976 2018-11-18 06:02 当我们使用支持 PD 或者 QC 快充协议的电源适 ...
- 长乐培训Day8
T1 远征 题目 [题目描述] 寒枫将军将要带领他的部队去圣雪山消灭那里的冰龙.部队分成了若干个小队,属于同一个小队的人兵种相同. 寒枫将军有着杰出的指挥能力,在战斗的时候,寒枫将军能够让所有相同兵种 ...
- python学习-51 shelve模块
import shelve f=shelve.open(r'shelve') #f['stu_info'] = {'name':'john','age':'18'} #f['stu2_info'] = ...
- TZOJ3133: 求对称点
#include<stdio.h> int main() { double a,b,c,d,e,f,g,h,i; while(~scanf("%lf %lf %lf %lf %l ...
- SRID (空间引用识别号, 坐标系)【转】
SRID (空间引用识别号, 坐标系)-云栖社区-阿里云 Spatial Reference List -- Spatial Reference Chapter 8. PostGIS Referenc ...