打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
C#中获取当前时间方法

--DateTime 数字型
System.DateTime currentTime=new System.DateTime();
取当前年月日时分秒      currentTime=System.DateTime.Now;
取当前年     int 年=currentTime.Year;
取当前月     int 月=currentTime.Month;
取当前日     int 日=currentTime.Day;
取当前时     int 时=currentTime.Hour;
取当前分     int 分=currentTime.Minute;
取当前秒     int 秒=currentTime.Second;
取当前毫秒    int 毫秒=currentTime.Millisecond; (变量可用中文)

取中文日期显示——年月日时分    string strY=currentTime.ToString("f"); //不显示秒

取中文日期显示_年月       string strYM=currentTime.ToString("y");

取中文日期显示_月日     string strMD=currentTime.ToString("m");

取当前年月日,格式为:2003-9-23      string strYMD=currentTime.ToString("d");

取当前时分,格式为:14:24      string strT=currentTime.ToString("t");

DateTime.Now.ToString();//获取当前系统时间完整的日期和时间
DateTime.Now.ToLongDateString();//
只显示日期 xxxx年xx月xx日,一个是长日期
DateTime.Now.ToShortDateString();//只显示日期 xxxx-xx-xx 一个是短日期

//今天        DateTime.Now.Date.ToShortDateString();
//昨天的       DateTime.Now.AddDays(-1).ToShortDateString();
//明天的       DateTime.Now.AddDays(1).ToShortDateString();


//本周(注意这里的每一周是从周日始至周六止)
DateTime.Now.AddDays(Convert.ToDouble((0 - Convert.ToInt16(DateTime.Now.DayOfWeek)))).ToShortDateString();
DateTime.Now.AddDays(Convert.ToDouble((6 - Convert.ToInt16(DateTime.Now.DayOfWeek)))).ToShortDateString();

//上周,上周就是本周再减去7天

DateTime.Now.AddDays(Convert.ToDouble((0 - Convert.ToInt16(DateTime.Now.DayOfWeek))) - 7).ToShortDateString();
DateTime.Now.AddDays(Convert.ToDouble((6 - Convert.ToInt16(DateTime.Now.DayOfWeek))) - 7).ToShortDateString();

//下周    本周再加上7天

DateTime.Now.AddDays(Convert.ToDouble((0 - Convert.ToInt16(DateTime.Now.DayOfWeek))) + 7).ToShortDateString();
   DateTime.Now.AddDays(Convert.ToDouble((6 - Convert.ToInt16(DateTime.Now.DayOfWeek))) + 7).ToShortDateString();

//本月    本月的第一天是1号,最后一天就是下个月一号再减一天。

DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + "1"; //第一天
DateTime.Parse(DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + "1").AddMonths(1).AddDays(-1).ToShortDateString();//最后一天

另一种方法:

DateTime now = DateTime.Now;
DateTime d1 = new DateTime(now.Year, now.Month, 1); //本月第一天

DateTime d2 = d1.AddMonths(1).AddDays(-1); //本月最后一天

PS:

 

DateTime.Now.DayOfWeek.ToString();//英文星期显示,Wednesday

(int)DateTime.Now.DayOfWeek     数字,若是周三,结果对应为3

DateTime.Now.ToString("dddd", new System.Globalization.CultureInfo("zh-cn")); //中文星期显示
DateTime.Now.ToString("dddd");//中文星期显示

DateTime.Now.ToString("dddd,MMMM,dd ,yyyy", new System.Globalization.DateTimeFormatInfo());//显示日期格式Friday,July, 01,2009

DateTime.Now.ToString("dddd,dd MMMM,yyyy") //输出   星期三,30 一月,2008


本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
C#里巧用DateTime预设一些可选的日期范围(如本年度、本季度、本月等) - C# W...
DateTime.Now.ToString() 用法
用于查询的日期类型转换帮助类
Datetime 在C#中的用法 获取当前时间的各种格式
Repeater绑定ItemDataBound的事件处理 Repeater1
ToShortDateString() .
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服