Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7905013
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T10:17:52+00:00 2026-06-03T10:17:52+00:00

Here is my code: DateTime startDate = Convert.ToDateTime( orderDate ).AddMonths(-1); DateTime endDate = Convert.ToDateTime(

  • 0

Here is my code:

DateTime startDate = Convert.ToDateTime( orderDate ).AddMonths(-1);
DateTime endDate = Convert.ToDateTime( orderDate );

orderDate is a valid date I pass in.

How do I always guarantee startDate is the first day of the previous month for orderDate?
How do I always guarantee endDate is the last day of the month for orderDate?

Example:

orderDate = 5/4/2012

I want startDate to be 4/1/2012 (or 04/1/2012 whichever is default)
I want endDate to be 4/30/2012

How can I acheive this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T10:17:53+00:00Added an answer on June 3, 2026 at 10:17 am
    DateTime startDate = Convert.ToDateTime( orderDate ).AddMonths(-1);
    // set to first of month
    startDate = startDate.AddDays(1-startDate.Day);
    
    // Set end date to last of month, which is one day before first of next month
    DateTime endDate = startDate.AddMonths(1).AddDays(-1);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What am I doing wrong here? Declare @starttimestamp datetime = getdate(); RAISERROR(N'Code not valid
I have this code: public IEnumerable<SomeClass> GetAvalibleThingies(DateTime startDate, DateTime endDate, int categoryID) { if
Here's my code: DateTime date1 = new DateTime(byear, bmonth, bday, 0, 0, 0); DateTime
Here is my code: a.dateFrom = DateTime.ParseExact(x, dd/mm/yyyy, null); And x has value of:
Here is my code: partial void OnisApprovedChanging(bool value) { this.dateApproved = DateTime.Now; } 'dateApproved'
I've got the following code: int bookingType=0; //mapSize=mapLoaded.size(); DateTime startx = new DateTime(startDate.getTime()); DateTime
I have a DataTable which has the following structure. StartDate (Type DateTime) EndDate (Type
I have no idea why exception was thrown, here's the working code: DateTime.Parse(1/12/2012 12:00:00
everyone! )) Here is code. var existingEntities = (from record in globalOne.serviceContext.records_out where record.timestamp.Date
Ok the error is showing up somewhere in this here code if($error==false) { $query

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.