使用C#代码获取日期 [英] get the date using c# code

查看:83
本文介绍了使用C#代码获取日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我们以

label1.text=datetime.now.tostring();


的形式显示当前日期 那么我们将在2011/3/18下午12:01:10
我想显示上个月的21日,怎么办?

Label2.text =?
表示2011年2月21日12:01:10 Pm如何运行?


请发送c#代码非常快


then we get 3/18/2011 12:01:10 PM
I want to display previous month 21 st date ,How?

Label2.text=?
Means 2/21/2011 12:01:10 Pm How?


please send the c# Code Urgently Fast

推荐答案

DateTime date = DateTime.Now.AddMonths(-1);
DateTime date2 = new DateTime(date.Year, date.Month, 21, date.Hour, date.Minute, date.Second);
label1.Text = date2.ToString();


或者:


or alternatively:

DateTime now = DateTime.Now;
DateTime date = now.AddMonths(-1);
int days = DateTime.DaysInMonth(date.Year, date.Month);
DateTime date2 = now.AddDays(-days - now.Day + 21);
label1.Text = date2.ToString();


您可以使用DateTime构造函数的第6个重载

DateTime dt = new DateTime(2011,2,21,12,1,10);
Label1.Text = dt.ToString();
You can use DateTime Constructor''s 6th overload

DateTime dt=new DateTime(2011,2,21,12,1,10);
Label1.Text=dt.ToString();


您可以使用
You can use the appropriate constructor of the DateTime[^] structure and display it.

You are not mentioning about any rules for deriving the date. If there is any use the AddDays method accordingly.


这篇关于使用C#代码获取日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆