tags:

views:

1718

answers:

3

input 15:20:30

to to convert to second

A: 
var dt = DateTime.Now;
var ticks = dt.Ticks;
var seconds = ticks/10000000;

Each tick is 100 nanoseconds.

Richard
Well, that's the no of seconds since 12:00:00 midnight, January 1, 0001 - are you sure that's what he wants?
UpTheCreek
Why do you assume that he asked for seconds since DateTime.MinValue?
Stefan Steinegger
I don't think the questioner knows what he wants :-) So the answer is deliberately incomplete --- it is all about the Ticks property and converting it to seconds.
Richard
+11  A: 

Seeing as though you haven't specified the question properly I have interpreted it to represent 15 hours 20 minutes and 30 seconds, as opposed to DateTime.Now. (Obviously this is the same as "How many seconds since midnight")

  TimeSpan MySpan = new TimeSpan(15, 20, 30);
  MySpan.TotalSeconds;

Although if you're only wanting the Seconds from the current DateTime.Now (this is not the TotalSeconds, just the current minutes seconds), just use:

  DateTime.Now.Second
ThePower
Is there a DataTime.LifeStart which gets the time of the start of life 4.5 billion years (or so) ago? Seen as the question is so vague.
kjfletch
I have created my own DateTime object and appended a property called JesusStart that returns new DateTime(0000, 12, 25);
ThePower
I have the same property. But it just raises AgentDoesNotExistError.
kjfletch
+1  A: 

Not sure what you really want, but if you want to compute the number of seconds from 15 hours, 20 minutes and 30 seconds you can do this:

Int32 GetSeconds(Int32 hours, Int32 minutes, Int32 seconds) {
  return ((hours*60) + minutes)*60 + seconds;
}
Martin Liversage
You can use this in any language. I know it is very simple, but my assumption is that the question also is very simple.
Martin Liversage
Fair point!
ThePower