当前位置: 移动技术网 > IT编程>开发语言>.net > DateTime Tips

DateTime Tips

2019年01月12日  | 移动技术网IT编程  | 我要评论

咆哮星际,中国无产阶级最早诞生于,乐之邦

datetime tips(system.runtime version=4.2.1.0

抛砖引玉,如有错误或是更好的方式还望不吝赐教

1. 根据某个datetime对象取其当天的起始时间
例如:
输入是 2019-01-14t01:27:59.260z
输出为 2019-01-14t00:00:00.000z

var result = sourcedate.date;  //sourcedate为datetime类型


另,如果是当日时间可以直接使用 date.today

 

2. am与pm
12:00am是午夜
12:00pm是正午

14日 12:00am 这个时间使用 addhours(-1)得到的是13日11:00pm
14日 12:00am 这个时间使用 addhours(1) 得到的是14日1:00am

 

3. 每周的开始时间

有时候我们需要自定义每周从哪一天开始(例如:国外周六,国内周一。某x石传说周四更新乱斗等等)

先来看看.net core 自带的dayofweek枚举

    //
    // 摘要:
    //     specifies the day of the week.
    public enum dayofweek
    {
        sunday = 0,
        monday = 1,
        tuesday = 2,
        wednesday = 3,
        thursday = 4,
        friday = 5,
        saturday = 6
    }

 

// 使用dayofweek中的值,这里配置每周从周几开始,如周四weekbeginday=4
var weekbeginday = 1;

// 确定给定日期在dayofweek的order,此处使用系统当日日期
var order = (int)datetime.today.dayofweek;

// 计算修正日期,如果给定日期(以系统今日时间today为例)order大于等于一周开始时间的order, 则修正值为给定日期order与一周开始时间的order的差。
// 如周一为每周开始时间,则周四时修正值为4-1=3.
// 如果给定日期order小于一周开始时间的order,则需额外加7
correctioncount = todayorder < weekbeginday ? todayorder - weekbeginday + 7 : todayorder - weekbeginday

// 所以给定日期所在周期的开始结束时间分别为
var weekbegin = datetime.today.adddays(0 - correctioncount);
var weekend = datetime.today.adddays(7 - correctioncount);

 

4.园子里pickuper的c#语言之“string格式的日期时间字符串转为datetime类型”的方法讲的很好,只是最后部分的格式有点乱了,整理如下

// 日期格式:yyyymmdd hh:mm:ss(注意此字符串的字母大小写很严格)yyyy代表年份mm代表月份dd代表天hh代表小时(24小时制)mm代表分钟ss代表秒

datetime.now.toshorttimestring();
datetime dt = datetime.now;
dt.tostring();//2005-11-5 13:21:25
dt.tofiletime().tostring();//127756416859912816
dt.tofiletimeutc().tostring();//127756704859912816
dt.tolocaltime().tostring();//2005-11-5 21:21:25
dt.tolongdatestring().tostring();//2005年11月5日
dt.tolongtimestring().tostring();//13:21:25
dt.tooadate().tostring();//38661.5565508218
dt.toshortdatestring().tostring();//2005-11-5
dt.toshorttimestring().tostring();//13:21
dt.touniversaltime().tostring();//2005-11-5 5:21:25
dt.year.tostring();//2005
dt.date.tostring();//2005-11-5 0:00:00
dt.dayofweek.tostring();//saturday
dt.dayofyear.tostring();//309
dt.hour.tostring();//13
dt.millisecond.tostring();//441
dt.minute.tostring();//30
dt.month.tostring();//11
dt.second.tostring();//28
dt.ticks.tostring();//632667942284412864
dt.timeofday.tostring();//13:30:28.4412864
dt.tostring();//2005-11-5 13:47:04
dt.addyears(1).tostring();//2006-11-5 13:47:04
dt.adddays(1.1).tostring();//2005-11-6 16:11:04
dt.addhours(1.1).tostring();//2005-11-5 14:53:04 多了1小时6分钟 此处是支持double类型的
dt.addmilliseconds(1.1).tostring();//2005-11-5 13:47:04
dt.addmonths(1).tostring();//2005-12-5 13:47:04
dt.addseconds(1.1).tostring();//2005-11-5 13:47:05 ,此处直接使用tostring被精度限制了
dt.addminutes(1.1).tostring();//2005-11-5 13:48:10
dt.addticks(1000).tostring();//2005-11-5 13:47:04
dt.compareto(dt).tostring();//0
dt.add(?).tostring();//问号为一个时间段
dt.equals("2005-11-6 16:11:04").tostring();//false
dt.equals(dt).tostring();//true
dt.gethashcode().tostring();//1474088234
dt.gettype().tostring();//system.datetime
dt.gettypecode().tostring();//datetime
dt.getdatetimeformats('s')[0].tostring();//2005-11-05t14:06:25
dt.getdatetimeformats('t')[0].tostring();//14:06
dt.getdatetimeformats('y')[0].tostring();//2005年11月
dt.getdatetimeformats('d')[0].tostring();//2005年11月5日
dt.getdatetimeformats('d')[1].tostring();//2005 11 05
dt.getdatetimeformats('d')[2].tostring();//星期六 2005 11 05
dt.getdatetimeformats('d')[3].tostring();//星期六 2005年11月5日
dt.getdatetimeformats('m')[0].tostring();//11月5日
dt.getdatetimeformats('f')[0].tostring();//2005年11月5日 14:06
dt.getdatetimeformats('g')[0].tostring();//2005-11-5 14:06
dt.getdatetimeformats('r')[0].tostring();//sat, 05 nov 2005 14:06:25 gmt
string.format("{0:d}",dt);//2005-11-5 string.format("{0}",dt);//2005年11月5日
string.format("{0:f}",dt);//2005年11月5日 14:23
string.format("{0:f}",dt);//2005年11月5日 14:23:23
string.format("{0:g}",dt);//2005-11-5 14:23
string.format("{0:g}",dt);//2005-11-5 14:23:23
string.format("{0:m}",dt);//11月5日
string.format("{0:r}",dt);//sat, 05 nov 2005 14:23:23 gmt
string.format("{0:s}",dt);//2005-11-05t14:23:23
string.format("{0:t}",dt);//14:23
string.format("{0:t}",dt);//14:23:23
string.format("{0:u}",dt);//2005-11-05 14:23:23z
string.format("{0:u}",dt);//2005年11月5日 6:23:23
string.format("{0:y}",dt);//2005年11月
string.format("{0}",dt);//2005-11-5 14:23:23
string.format("{0:yyyymmddhhmmssffff}",dt);

// 计算2个日期之间的天数差 ----------------------------------------------- 
datetime dt1 = convert.datetime("2007-8-1");
datetime dt2 = convert.datetime("2007-8-15");
timespan span = dt2.subtract(dt1);
int daydiff = span.days + 1;

// 计算某年某月的天数 -----------------------------------------------
int days = datetime.daysinmonth(2007, 8); //days = 31;

// 给日期增加一天、减少一天 -----------------------------------------------
datetime dt =datetime.now;
dt.adddays(1);//增加一天
dt.adddays(-1);//减少一天 其它年份方法类似...

// oracle sql里转换日期函数 -----------------------------------------------
to_date("2007-6-6",'yyyy-mm-dd");
to_date("2007/6/6",'yyyy/mm/dd");

 

另:

dt.addhours(1.1).tostring();//2005-11-5 14:53:04 多了1小时6分钟 此处是支持double类型的

 

并非所有add~系列的方法均支持double类型的,

具体为:

public datetime add(timespan value);
public datetime adddays(double value);
public datetime addhours(double value);
public datetime addmilliseconds(double value);
public datetime addminutes(double value);
public datetime addmonths(int months);
public datetime addseconds(double value);
public datetime addticks(long value);
public datetime addyears(int value);

 

以上。

 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网