Solved

C# DateTime from Month and Year

Posted on 2015-02-23
4
157 Views
Last Modified: 2015-02-24
Hello all,

I have the following,  Month Abbreviation such as Jan and Year such as 2015 in a string "Jan 2015".   I need to take that and get the 1st and last day of that month into two datetime variables.  

I got the month abbreviation using:

DateTimeFormatInfo mfi = new DateTimeFormatInfo();
mfi.GetAbbreviatedMonthName(1) as an example.

I guess I need to parse the numeric and string portion to do this?   So in the above case Jan 2015 I would want two datetime variables as:

DateTime startDate = 1/12015
DateTime endDate = 1/31/2015
0
Comment
Question by:sbornstein2
  • 2
4 Comments
 
LVL 12

Assisted Solution

by:FarWest
FarWest earned 150 total points
ID: 40626795
check this code
      static void Main(string[] args)
        {
            string mmmyyyy = Console.ReadLine();// enter date ex feb 2015
            DateTime sdate = Convert.ToDateTime("01 " + mmmyyyy);
            DateTime edate = sdate.AddMonths(1);
            edate = edate.AddDays(-1);
            Console.WriteLine("Start Date is " + sdate.ToString("MM/dd/yyyy"));
            Console.WriteLine("Start Date is " + edate.ToString("MM/dd/yyyy"));
            Console.ReadKey();

        }

Open in new window

0
 
LVL 49

Accepted Solution

by:
Gustav Brock earned 350 total points
ID: 40627677
You should use the very clever method Parse for this:

String monthYear = "Jan 2015";
DateTime startDate = DateTime.Parse(monthYear);
DateTime endDate = startDate.AddMonths(1).AddDays(-1);  

/gustav
0
 

Author Closing Comment

by:sbornstein2
ID: 40628228
ah very clever it is.   Did not know it could parse a month and year like that.  Thanks
0
 
LVL 49

Expert Comment

by:Gustav Brock
ID: 40628238
You are welcome!
.Net is magic.

/gustav
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
This video discusses moving either the default database or any database to a new volume.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now