How to Extract current date, parse it and add 7 days
I'd like to do this :
- Extract the current date;
- Parse it as DD/MM/YYYY;
- Add to it 7 days;
How can I do it on C#?
I hope there are specific method开发者_如何转开发s for do it (without extraxt, split, arrange arrays, join, ecc...).
SOLUTION TAKEN
DateTime dt = DateTime.Parse(System.DateTime.Now.ToString());
txtArrivo.Text = dt.ToString("dd/MM/yyyy");
txtPartenza.Text = dt.AddDays(7).ToString("dd/MM/yyyy");
It is as simple as that:
DateTime inSevenDays = DateTime.Today.AddDays(7);
No need to parse anything.
I'm not quite sure what you mean by "Extract the actual data". But I'm assuming your getting the value back in a string form. If so then you can do the following
string data = ...;
DateTime date = DateTime.Parse(data).AddDays(7);
Or to be more precise you can do the following
string data = ...;
DateTime date = DateTime.ParseExact(
data,
"dd/MM/yyyy",
CultureInfo.InvariantCulture).AddDays(7);
Use DateTime to parse it and then use AddDays(7) to add 7 days to the DateTime object
Here you go for Ex st is the string where you have the date:
string st = "12/01/2011";
DateTime dt = DateTime.Parse(st).AddDays(7);
Console.Write(dt.ToString("MM/dd/yyyy");
CultureInfo provider = CultureInfo.InvariantCulture;
string dateString = "05/01/2009";
try {
dateValue = DateTime.ParseExact(dateString, "dd/MM/yyyy", provider);
dateValue = dateValue.AddDays(7);
}
catch {
// something wrong
}
If you are sure that the dates are always in DD/MM/YYYY format, then use:
DateTime date = DateTime.ParseExact(dateString, "dd/MM/yyyy", null).AddDays(7);
精彩评论