Solved

C# DateTime from Month and Year

Posted on 2015-02-23
4
165 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
ConsoleSql 1 34
This code tracks birthdays 3 56
Entity Framework 7 28
C#.NET and microsoft certification. 3 25
Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
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: …

914 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

13 Experts available now in Live!

Get 1:1 Help Now