Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder complete's Avatar
    Join Date
    Jul 2005
    Location
    USA
    Posts
    87
    Thanks
    2
    Thanked 0 Times in 0 Posts

    How do I get a dattime format in C# that will look like this:

    How do I get a dattime format in C# that will look like this:

    2011-09-01 00:00:00:000
    This works
    DateTime saveNow = DateTime.Now;

    string test = String.Format("{0:yyyy-MM-01 00:00:00:000}", saveNow);

    Now I want to know this. How would I subtract one month from the current month?

    It T-SQL it would look like this

    How would it be done in C#?


    trying the addmonth method with a negative one did not work

  • #2
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    Thanks
    1
    Thanked 328 Times in 324 Posts
    The reason the call to AddMonths didn't work is that the function doesn't change the instance of DateTime you call it on. Instead it returns a new DateTime instead with the result.

    So if you didn't care about saving the value of saveNow, you'd just do:
    Code:
    saveNow = saveNow.AddMonths(-1);
    OracleGuy


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •