每年的动态日期 [英] Dynamic Date for every year
问题描述
尊敬的先生/女士,
我的项目中有一个日期文本框.我的要求是截至3月31日的默认日期.我需要逐年动态更改今年.
例如
1.如果我的加入日期是14-02-2012,它将显示在文本框中31-03-2011,
如果我的加入日期是2012年1月4日,它将显示在文本框中31-03-2012,
注意:
1.这将在页面加载时发生.
Dear Sir/Madam,
I have a date textbox in my project. My requirement is Default date for end year March 31. I need to change this year dynamically year by year.
For Example
1.If My date of joining is 14-02-2012,it will show in the textbox 31-03-2011,
If My date of joining is 01-04-2012, it will show in the textbox 31-03-2012,
Note:
1.This will happen on page load
推荐答案
这很简单.您可以使用DateTime构造函数来初始化日期,方法是分别提供年,月和日值,如以下示例所示.
It is very simple. You can initiate date with DateTime constructor by providing year, month and day values separately as shown in following example.
DateTime dateTime=new DateTime((DOJ.Month>3?DOJ.Year:DOJ.Year-1), 3, 31);
希望有帮助!
Hope this help!
您好,
以下代码可能会帮助您:-
Hi,
Following code may help you:-
int icurrMonth = DateTime.Now.Month;
string sYear="";
if (icurrMonth < 4)//If joining month is April or less then Previous year
{
sYear = (DateTime.Now.Year - 1).ToString();
}
else //After April Current Year
{
sYear = DateTime.Now.Year.ToString();
}
txtBox.Text = "31-03-" + sYear;
注意:1.将"txtBox"替换为"TextBox ID"
2.将"DateTime.Now"替换为加入日期"
Note : 1. Replace "txtBox" with your "TextBox ID"
2. Replace "DateTime.Now" with "Date of Joining"
这篇关于每年的动态日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!