Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

C# DateTime from Month and Year

Posted on 2015-02-23
4
Medium Priority
?
211 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 12

Assisted Solution

by:FarWest
FarWest earned 600 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 51

Accepted Solution

by:
Gustav Brock earned 1400 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 51

Expert Comment

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

/gustav
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

688 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