locked
How to add Minutes in Datetime format RRS feed

  • Question

  • User-1305878657 posted

    Hi team ,

    I am using this format of datetime i want to addmiutes in this datetime format it gives error String was not recognized as a valid DateTime.

    System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss,fff");

    so kindly help me how i can do this

    thanks in adv

    Friday, June 24, 2016 10:32 AM

Answers

  • User541108374 posted

    Hi,

    this is not working

    It is, tested it before I wrote it down.

    my requirement  is that i m already having that format in my date variable then after how can add the minutes in that format 

    Well, if you already made it a string then before you can add minutes to it you'll have to transform it back to a valid DateTime instance, call AddMinutes on it and then perform a ToString on it again with the formatting.

    Grz, Kris.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, June 24, 2016 10:59 AM
  • User753101303 posted

    Hi,

    i m already having that format in my date variable

    Then it seems you have a string and that you try to add minutes to a string. Always work with the native type as long as you can. Transform the value to a string when you are done will all native operations and you are about to show the value to the user, write the value to a file or whatever...

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, June 24, 2016 1:14 PM

All replies

  • User541108374 posted

    Hi,

    you already made a string out of it so can't use DateTime members on that anymore.

    You can make it like:

    		var now = DateTime.Now;
    		now.AddMinutes(13);
    		now.ToString("yyyy-MM-dd HH:mm:ss,fff");

    So first create a variable that holds the first time, then perform adding minutes and when you have that result write it out as a formatted string.

    Grz, Kris.

    Friday, June 24, 2016 10:43 AM
  • User-1305878657 posted

    Thanks for quick reply

    but this is not working .actually my requirement  is that i m already having that format in my date variable then after how can add the minutes in that format 

    yyyy-MM-dd HH:mm:ss,fff

    thanks again ..

    Friday, June 24, 2016 10:56 AM
  • User541108374 posted

    Hi,

    this is not working

    It is, tested it before I wrote it down.

    my requirement  is that i m already having that format in my date variable then after how can add the minutes in that format 

    Well, if you already made it a string then before you can add minutes to it you'll have to transform it back to a valid DateTime instance, call AddMinutes on it and then perform a ToString on it again with the formatting.

    Grz, Kris.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, June 24, 2016 10:59 AM
  • User753101303 posted

    Hi,

    i m already having that format in my date variable

    Then it seems you have a string and that you try to add minutes to a string. Always work with the native type as long as you can. Transform the value to a string when you are done will all native operations and you are about to show the value to the user, write the value to a file or whatever...

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, June 24, 2016 1:14 PM