请教can't comparepython str datetimee.date to str的错误提示

The page is temporarily unavailable
nginx error!
The page you are looking for is temporarily unavailable.
Please try again later.
Website Administrator
Something has triggered an error on your
This is the default error page for
nginx that is distributed with
It is located
/usr/share/nginx/html/50x.html
You should customize this error page for your own
site or edit the error_page directive in
the nginx configuration file
/etc/nginx/nginx.conf.The page is temporarily unavailable
nginx error!
The page you are looking for is temporarily unavailable.
Please try again later.
Website Administrator
Something has triggered an error on your
This is the default error page for
nginx that is distributed with
It is located
/usr/share/nginx/html/50x.html
You should customize this error page for your own
site or edit the error_page directive in
the nginx configuration file
/etc/nginx/nginx.conf.The page is temporarily unavailable
nginx error!
The page you are looking for is temporarily unavailable.
Please try again later.
Website Administrator
Something has triggered an error on your
This is the default error page for
nginx that is distributed with
It is located
/usr/share/nginx/html/50x.html
You should customize this error page for your own
site or edit the error_page directive in
the nginx configuration file
/etc/nginx/nginx.conf.c#详解datetime使用示例
字体:[ ] 类型:转载 时间:
本文主要介绍了c# datetime使用示例,大家参考使用吧
用户输入一个日期,要求输出这个日期是星期几和在这一年中的第几天: 代码如下://声明一个DateTime类型的变量用于存放用户输入的日期DateTConsole.WriteLine("请输入日期:(例如: 或 )");//把输入的日期字符串转换成日期格式类型dt = DateTime.Parse(Console.ReadLine());//因为DayOfWeek返回的是0、1、2、3、4、5、6,分别对应的是日、一、二、三、四、五、六//Substring是进行检索字符串并返回匹配的指定长度的子字符串string str = "日一二三四五六".Substring((int)dt.DayOfWeek, 1);Console.WriteLine("{0}年{1}月{2}日是星期{3}", dt.Year, dt.Month, dt.Day, str);Console.WriteLine("{0}年{1}月{2}日是这一年的第{3}天", dt.Year, dt.Month, dt.Day, dt.DayOfYear);
Console.WriteLine("{0}是星期{1}", dt.ToShortDateString(), str);Console.WriteLine("{0}是这一年的第{1}天", dt.ToLongDateString(), dt.DayOfYear);
上述代码运行的结果如下
显示当前时期和时间的不同合格: 代码如下://以不同的格式显示当天日期和时间Console.WriteLine("现在时间是:{0}", DateTime.Now.ToString("yyyy-M-d H:m:s"));Console.WriteLine("现在时间是:{0}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));Console.WriteLine("现在时间是:{0}", DateTime.Now.ToString("yyyy-MM-dd"));
Console.WriteLine("短日期字符串表示现在时间是:{0}", DateTime.Now.Date.ToShortDateString());Console.WriteLine("长日期字符串表示现在时间是:{0}", DateTime.Now.Date.ToLongDateString ());
上述代码的运行结果如下
日期类型(DateTime)格式化
方法一:使用DateTime.ToString(format, DateTimeFormatInfo.InvariantInfo)示例代码: 代码如下:&DateTime dt = DateTime.N&t.ToString("M/d/yyyy");
参数format格式详细用法:
格式字符关联属性/说明dShortDatePatternDLongDatePatternf完整日期和时间(长日期和短时间)FFullDateTimePattern(长日期和长时间)g常规(短日期和短时间)G常规(短日期和长时间)m、MMonthDayPatternr、RRFC1123Patterns使用当地时间的SortableDateTimePattern(基于ISO8601)tShortTimePatternTLongTimePatternuUniversalSortableDateTimePattern用于显示通用时间的格式U使用通用时间的完整日期和时间(长日期和长时间)y、YYearMonthPattern
下面列出可被合并以构造自定义模式的模式:
d月中的某一天。一位数的日期没有前导零。dd月中的某一天。一位数的日期有一个前导零。ddd周中某天的缩写名称,在AbbreviatedDayNames中定义。dddd周中某天的完整名称,在DayNames中定义。M月份数字。一位数的月份没有前导零。MM月份数字。一位数的月份有一个前导零。MMM月份的缩写名称,在AbbreviatedMonthNames中定义。MMMM月份的完整名称,在MonthNames中定义。y不包含纪元的年份。如果不包含纪元的年份小于10,则显示不具有前导零的年份。yy不包含纪元的年份。如果不包含纪元的年份小于10,则显示具有前导零的年份。yyyy包括纪元的四位数的年份。gg时期或纪元。如果要设置格式的日期不具有关联的时期或纪元字符串,则忽略该模式。h12小时制的小时。一位数的小时数没有前导零。hh12小时制的小时。一位数的小时数有前导零。H24小时制的小时。一位数的小时数没有前导零。HH24小时制的小时。一位数的小时数有前导零。m分钟。一位数的分钟数没有前导零。mm分钟。一位数的分钟数有一个前导零。s秒。一位数的秒数没有前导零。ss秒。一位数的秒数有一个前导零。f秒的小数精度为一位。其余数字被截断。ff秒的小数精度为两位。其余数字被截断。fff秒的小数精度为三位。其余数字被截断。ffff秒的小数精度为四位。其余数字被截断。fffff秒的小数精度为五位。其余数字被截断。ffffff秒的小数精度为六位。其余数字被截断。fffffff秒的小数精度为七位。其余数字被截断。t在AMDesignator或PMDesignator中定义的AM/PM指示项的第一个字符(如果存在)。tt在AMDesignator或PMDesignator中定义的AM/PM指示项(如果存在)。z时区偏移量(“+”或“-”后面仅跟小时)。一位数的小时数没有前导零。例如,太平洋标准时间是“-8”。zz时区偏移量(“+”或“-”后面仅跟小时)。一位数的小时数有前导零。例如,太平洋标准时间是“-08”。zzz完整时区偏移量(“+”或“-”后面跟有小时和分钟)。一位数的小时数和分钟数有前导零。例如,太平洋标准时间是“-08:00”。:在TimeSeparator中定义的默认时间分隔符。/在DateSeparator中定义的默认日期分隔符。%c其中c是格式模式(如果单独使用)。如果格式模式与原义字符或其他格式模式合并,则可以省略“%”字符。\c其中c是任意字符。照原义显示字符。若要显示反斜杠字符,请使用“\\”。
代码如下:DateTimedt=DateTime.NLabel1.Text=dt.ToString();//:21:25Label4.Text=dt.ToLocalTime().ToString();//:21:25Label5.Text=dt.ToLongDateString().ToString();//日Label6.Text=dt.ToLongTimeString().ToString();//13:21:25dt.ToString();//:21:25dt.ToFileTime().ToString();//912816dt.ToFileTimeUtc().ToString();//912816dt.ToLocalTime().ToString();//:21:25dt.ToLongDateString().ToString();//日dt.ToLongTimeString().ToString();//13:21:25dt.ToOADate().ToString();//8218dt.ToShortDateString().ToString();//dt.ToShortTimeString().ToString();//13:21dt.ToUniversalTime().ToString();//:21:25dt.Year.ToString();//2005dt.Date.ToString();//:00:00dt.DayOfWeek.ToString();//Saturdaydt.DayOfYear.ToString();//309dt.Hour.ToString();//13dt.Millisecond.ToString();//441dt.Minute.ToString();//30dt.Month.ToString();//11dt.Second.ToString();//28dt.Ticks.ToString();//412864dt.TimeOfDay.ToString();//13:30:28.4412864dt.ToString();//:47:04dt.AddYears(1).ToString();//:47:04dt.AddDays(1.1).ToString();//:11:04dt.AddHours(1.1).ToString();//:53:04dt.AddMilliseconds(1.1).ToString();//:47:04dt.AddMonths(1).ToString();//:47:04dt.AddSeconds(1.1).ToString();//:47:05dt.AddMinutes(1.1).ToString();//:48:10dt.AddTicks(1000).ToString();//:47:04<pareTo(dt).ToString();//0dt.Add(?).ToString();//问号为一个时间段dt.Equals(":11:04").ToString();//Falsedt.Equals(dt).ToString();//Truedt.GetHashCode().ToString();//dt.GetType().ToString();//System.DateTimedt.GetTypeCode().ToString();//DateTimedt.GetDateTimeFormats('s')[0].ToString();//T14:06:25dt.GetDateTimeFormats('t')[0].ToString();//14:06dt.GetDateTimeFormats('y')[0].ToString();//2005年11月dt.GetDateTimeFormats('D')[0].ToString();//日dt.GetDateTimeFormats('D')[1].ToString();//dt.GetDateTimeFormats('D')[2].ToString();//星期六dt.GetDateTimeFormats('D')[3].ToString();//星期六日dt.GetDateTimeFormats('M')[0].ToString();//11月5日dt.GetDateTimeFormats('f')[0].ToString();//datetime操作//今天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();//如果你还不明白,再看一下中文显示星期几的方法就应该懂了//由于DayOfWeek返回的是数字的星期几,我们要把它转换成汉字方便我们阅读,有些人可能会用switch来一个一个地对照,其实不用那么麻烦的string[]Day=newstring[]{"星期日","星期一","星期二","星期三","星期四","星期五","星期六"};Day[Convert.ToInt16(DateTime.Now.DayOfWeek)];//上周,同理,一个周是7天,上周就是本周再减去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();//下周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();//最后一天//巧用C#里ToString的字符格式化更简便DateTime.Now.ToString("yyyy-MM-01");DateTime.Parse(DateTime.Now.ToString("yyyy-MM-01")).AddMonths(1).AddDays(-1).ToShortDateString();//上个月,减去一个月份DateTime.Parse(DateTime.Now.ToString("yyyy-MM-01")).AddMonths(-1).ToShortDateString();DateTime.Parse(DateTime.Now.ToString("yyyy-MM-01")).AddDays(-1).ToShortDateString();//下个月,加去一个月份DateTime.Parse(DateTime.Now.ToString("yyyy-MM-01")).AddMonths(1).ToShortDateString();DateTime.Parse(DateTime.Now.ToString("yyyy-MM-01")).AddMonths(2).AddDays(-1).ToShortDateString();//7天后DateTime.Now.Date.ToShortDateString();DateTime.Now.AddDays(7).ToShortDateString();//7天前DateTime.Now.AddDays(-7).ToShortDateString();DateTime.Now.Date.ToShortDateString();//本年度,用ToString的字符格式化我们也很容易地算出本年度的第一天和最后一天DateTime.Parse(DateTime.Now.ToString("yyyy-01-01")).ToShortDateString();DateTime.Parse(DateTime.Now.ToString("yyyy-01-01")).AddYears(1).AddDays(-1).ToShortDateString();//上年度,不用再解释了吧DateTime.Parse(DateTime.Now.ToString("yyyy-01-01")).AddYears(-1).ToShortDateString();DateTime.Parse(DateTime.Now.ToString("yyyy-01-01")).AddDays(-1).ToShortDateString();//下年度DateTime.Parse(DateTime.Now.ToString("yyyy-01-01")).AddYears(1).ToShortDateString();DateTime.Parse(DateTime.Now.ToString("yyyy-01-01")).AddYears(2).AddDays(-1).ToShortDateString();//本季度,很多人都会觉得这里难点,需要写个长长的过程来判断。其实不用的,我们都知道一年四个季度,一个季度三个月//首先我们先把日期推到本季度第一个月,然后这个月的第一天就是本季度的第一天了DateTime.Now.AddMonths(0-((DateTime.Now.Month-1)%3)).ToString("yyyy-MM-01");//同理,本季度的最后一天就是下季度的第一天减一DateTime.Parse(DateTime.Now.AddMonths(3-((DateTime.Now.Month-1)%3)).ToString("yyyy-MM-01")).AddDays(-1).ToShortDateString();//下季度,相信你们都知道了。。。。收工DateTime.Now.AddMonths(3-((DateTime.Now.Month-1)%3)).ToString("yyyy-MM-01");DateTime.Parse(DateTime.Now.AddMonths(6-((DateTime.Now.Month-1)%3)).ToString("yyyy-MM-01")).AddDays(-1).ToShortDateString();//上季度DateTime.Now.AddMonths(-3-((DateTime.Now.Month-1)%3)).ToString("yyyy-MM-01");DateTime.Parse(DateTime.Now.AddMonths(0-((DateTime.Now.Month-1)%3)).ToString("yyyy-MM-01")).AddDays(-1).ToShortDateString();
您可能感兴趣的文章:
大家感兴趣的内容
12345678910
最近更新的内容
常用在线小工具The page is temporarily unavailable
nginx error!
The page you are looking for is temporarily unavailable.
Please try again later.
Website Administrator
Something has triggered an error on your
This is the default error page for
nginx that is distributed with
It is located
/usr/share/nginx/html/50x.html
You should customize this error page for your own
site or edit the error_page directive in
the nginx configuration file
/etc/nginx/nginx.conf.

我要回帖

更多关于 c datetime.compare 的文章

 

随机推荐