4

I have some DateTime's that will be calculated in the format: dtDateTime.ToString("M/dd/yyyy h:mm:ss tt") and I want to keep the ss formatting but just set the value to zero. Example:

Before Change:

"7/14/2014 7:34:27 AM"

After Change:

"7/14/2014 7:34:00 AM"

What I have tried:

dtDateTime = my DateTime
TimeSpan secondsDifference = dtDateTime.Subtract(new System.DateTime(0, 0, 0, 0, 0, dtDateTime.Second)
dtDateTime = dtDateTime.Date - secondsDifference;
return dtDateTime.ToString("M/dd/yyyy h:mm:ss tt")

This gives me an error, on the dtDateTime.Subtract() expression because this date is before 1970, when (I think) Unix Time stamping started. How can I simply set the seconds part of dtDateTime to 00?

NB - code brevity is important. Thanks all.

Mr. Boy
  • 60,845
  • 93
  • 320
  • 589
user3772119
  • 484
  • 3
  • 7
  • 16
  • 2
    [Build a new `DateTime` and hardcode the seconds.](http://msdn.microsoft.com/en-us/library/272ba130(v=vs.110).aspx) – Adam Houldsworth Jul 15 '14 at 15:24
  • possible duplicate of [how to change time in datetime](http://stackoverflow.com/questions/1859248/how-to-change-time-in-datetime) – Victor Zakharov Jul 15 '14 at 15:28
  • 1
    @Neolisk That SO post is what my original code was based off of (esp the `dtDateTime = dtDateTime.Date - secondsDifference;` line, but I knew there had to be a simpler way! – user3772119 Jul 15 '14 at 15:32

4 Answers4

23
myDate = myDate.AddSeconds(-myDate.Second)
ironzionlion
  • 832
  • 1
  • 7
  • 28
11

If the purpose is to only show 00 seconds then just replace ss with 00

dtDateTime.ToString("M/dd/yyyy h:mm:00 tt", CultureInfo.InvariantCulture)
Habib
  • 219,104
  • 29
  • 407
  • 436
  • @Habib What if we would like to **set** `00` seconds to `dt`? Here the console displays `00` but `dt` still keeps the assigned seconds... – ironzionlion Jul 16 '14 at 11:45
  • @ironzionlion, the OP's code has `return dtDateTime.ToString...` so the OP is returning a formatted string. There should be no need to set `dt`'s seconds to zero. – Habib Jul 16 '14 at 12:57
  • @Habib thanks for your comment. I am just wondering if there would be a wait to define a `DateTime` already with a fixed value, in that case, `ss`. Is there a way to define a `DateTime` value that stays constant, even if I try `dt = dt.AddSeconds(5)`? – ironzionlion Jul 16 '14 at 13:04
  • 1
    @ironzionlion, `DateTime` is immutable, So any change you made the object will return a new object. Therefore you can use **readonly** DateTime like: `readonly DateTime SomeDate = new DateTime(2014, 01, 01);` . It will not be a `const` but it will give you somewhat similar functionality – Habib Jul 16 '14 at 13:10
8

I haven't tried it but changing the format to "M/dd/yyyy h:mm:00 tt" should work.

Andy Jones
  • 584
  • 2
  • 14
4

You could try the following:

// create a new datetime based on this you have
DateTime dt = new DateTime(inputDate.Year, 
                           inputDate.Month, 
                           inputDate.Day, 
                           inputDay.Hour, 
                           inputDay.Minute, 
                           0);


// print it to the console
Console.WriteLine(t.ToString("M/dd/yyyy h:mm:ss tt"));
Christos
  • 53,228
  • 8
  • 76
  • 108