Click here to Skip to main content
12,884,261 members (30,036 online)
Rate this:
Please Sign up or sign in to vote.
See more: C#
if we display current date in the form of;

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

Means 2/21/2011 12:01:10 Pm How?

please send the c# Code Urgently Fast
Posted 17-Mar-11 20:39pm
Updated 18-Mar-11 6:01am
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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();
Toniyo Jackson 18-Mar-11 3:07am
Good answer. +5
Rate this: bad
Please Sign up or sign in to vote.

Solution 5

You can use DateTime Constructor's 6th overload

DateTime dt=new DateTime(2011,2,21,12,1,10);
Raj.rcr 18-Mar-11 4:03am
Good call. my 5
Sajid Ahmed Shahsroha 19-Mar-11 7:55am
whoever downvoted me plz could you tell me why ?? so that i can improve myself
Mark Nischalke 19-Mar-11 9:15am
Hard coding the values may solve this immediate problem but does nothing to provide a solution that will work in all cases.
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

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.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

    Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web01 | 2.8.170422.1 | Last Updated 18 Mar 2011
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100