Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
699 views
in Technique[技术] by (71.8m points)

c# - Is there any easy way to increment a DateTime by monthly/yearly/daily units without having to parse it out like crazy?

I need to set up billing cycles and process payments. So for example I will process a payment immediately and then set the next one up to process exactly one month from then.

So if I get DateTime.Now is there any quick way to just add a month to it? how about a year? Or will I need to parse it out into MM, YYYY, DD, and then add to MM, if MM == 12 increment year, etc... then piece it back together for my string to submit to this paypal plugin?

Here is the final format needed:

"YYYY-MM-DDTHH:MM:SS.MSZ".

This is explained in more detail below:




YYYY Four-digit year, e.g. "2005" 
MM  Two-digit month. 
DD Two-digit day. 
T Indicates time follows the date. 
HH Hours in military time (24-hour format). 
MM  Minutes 
SS Seconds 
MS Milliseconds 
Z 1-character (US military) representation of the time zone, "A" - "M" are negative offsets -1 to -12, with "J" not being used. "N" - "Y" are positive offsets 1 to 12, and "Z" indicates GMT/UTC (no offset).  


For instance, "2004-05-26T15:00:00.00Z" is May 26th, 2004 at 3:00pm GMT. 

So basically I am wondering if there are any easy built in ways to add one month or one year to a date without parsing the crap out of it as a string.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Try this:

DateTime myDateTime = DateTime.Now.AddMonths(1);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...