Why is this formatted result one day off?

  • Thread starter Thread starter Trint Smith
  • Start date Start date
T

Trint Smith

Ok, the 'SecondDate' comes from sql server, but I wanted to make it
visible here...but no matter what day I get in SecondDate, it comes up
one day off every time...the hours minutes and seconds are right:

Dim SecondDate As Date = "2/28/04 10:50:00 PM"
Dim FirstDate As Date
FirstDate = CDate(SecondDate)
Dim secs As Long = DateDiff(DateInterval.Second, Now,
SecondDate)
Dim dt As New DateTime(secs * 10000000D)
Dim dte As StreamWriter
dte = New StreamWriter("D:\datetimetest.txt", True)
dte.WriteLine(dt.ToString("dd HH:mm:ss"))
dte.Close()
Response.Write(dt.ToString("dd HH:mm:ss"))

Any help is greatly appreciated.
Thanks,
Trint

..Net programmer
(e-mail address removed)
 
Trint,

Somebody else may reply with the answer but meanwhile...

What's all the extra "stuff" for? If you don't need the StreamWriter in
order to demonstrate your problem it is much simpler for everybody if you
eliminate it as a source of your problem. I'm just scanning the code
quickly but what is FirstDate for? Why is it that you are casting
SecondDate (a date) to a Date isn't it already a date?

I don't even see what is "off" about it... what answer are you getting and
why do believe it isn't right?
 
Hi Trint,

Have a look at this, this is something as a sample that is very much near
what you need I think.

Module main
Public Sub main()
Dim starttime As DateTime = DateTime.Now
starttime = starttime.AddSeconds(10)
Dim span As TimeSpan = starttime.Subtract(Now)
Do While starttime > Now
MessageBox.Show(span.ToString)
Threading.Thread.Sleep(1000)
span = starttime.Subtract(Now)
Loop
End Sub
End Module

I hope this helps a little bit?

Cor
 
Cor,
I tried that with a little modification to my needs and the other is
still closer to what I need...here's why:
All I need is the 'time left: dd HH:mm:ss'.
ending datetime "2/28/04 10:50:00 PM" <-any future datetime
and NOW. The method of visual countdown on the screen will have to be
done by flash or java once I have this value.
Thanks,
Trint

.Net programmer
(e-mail address removed)
 
Trint,
I would use Integer (Long) arithmetic here instead of Decimal (does not
change result).
Dim dt As New DateTime(secs * 10000000D)
Dim dt As New DateTime(secs * 10000000L)


What exactly are you attempting to do? Based on what your example prints, I
would think a TimeSpan would work better.

Something like:
Dim secondDate As Date = "2/28/04 10:50:00 PM"
Dim firstDate As Date = DateTime.Now

Dim secs As Long = DateDiff(DateInterval.Second, firstDate,
secondDate)
Dim dt As New DateTime(secs * 10000000L)
Debug.WriteLine(dt.ToString("dd HH:mm:ss"), "DateDiff")

Dim ts As TimeSpan = secondDate.Subtract(firstDate)
Debug.WriteLine(String.Format("{0:D2} {1:D2}:{2:D2}:{3:D2}",
ts.Days, ts.Hours, ts.Minutes, ts.Seconds), "TimeSpan")

Unless of course you are expecting 10 instead of 8, then my example also has
an "issue" ;-)

Hope this helps
Jay
 
This is it for those following this:

Dim SecondDate As Date = "2/28/04 09:12:00 PM"
Dim FirstDate As Date = Now
Dim secs As Long = DateDiff(DateInterval.Second, Now,
SecondDate)

Dim ts As TimeSpan = TimeSpan.FromSeconds(secs)
Response.Write(ts.Days.ToString() & "d:" & ts.Hours.ToString() &
"h:" & ts.Minutes.ToString() & "m:" & ts.Seconds.ToString() & "s")

Thanks,
Trint

..Net programmer
(e-mail address removed)
 
Trint,
Why bother with the DateDiff function at all????
Dim SecondDate As Date = "2/28/04 09:12:00 PM"
Dim ts As TimeSpan = SecondDate.Subtract(Now)
Response.Write(ts.Days.ToString() & "d:" & ts.Hours.ToString() &
"h:" & ts.Minutes.ToString() & "m:" & ts.Seconds.ToString() & "s")

I don't really see that it (DateDiff) is adding any *value* to your
function, if any thing its making your function longer (and slower) then it
needs to be...

Hope this helps
Jay
 
Jay,
All I want is the time remaining and this was the only way I could
figure out that would do it.

Beginning DateTime:
Doesn't Matter

Ending DateTime:
2/28/04 09:12:00 PM

Now:
2/20/2004 11:26:49 AM

This comes from Diff and is what I needed:
Time Remaining:
8d:9h:45m:10s

Thanks,
Trint

..Net programmer
(e-mail address removed)
 
Hi Trint,

You give me really the idea that you not are looking to the help you get.

CJ was helping you and I was reading it, I got an idea from that, that I
would try.

Yesterday I made completly the routine that you wanted for a aspx server
application (although not complete, the bitmap is still to hugh I think but
it was an example).

You missed the countdown procedure, I made that today for you and then you
said you did not need that.

Jay B. ask you why you use the diff and not complet normal the timespan, and
then you give an answer what does sound for me as, "are you crazy this I
need, this is the best".

The procedure that you say now that you need is not more than this (as Jay B
stated).

Dim starttime As DateTime = DateTime.Now
starttime = CDate("02/28/04 09:12:00 PM")
Dim ts As TimeSpan = starttime.Subtract(Now)
MessageBox.Show(ts.Days.ToString() & "d:" & ts.Hours.ToString() & _
"h:" & ts.Minutes.ToString() & "m:" & ts.Seconds.ToString() & "s")

If you had told this directly, CJ could have written this I think directly
for you.

But I do not see the sample watch and the clock which is ticking in this.

Cor
 
Cor and CJ,
You give me really the idea that you not are looking to the help you
get.

If you had not given me the information you did, I would still be
starring at this and going crazy...I am using all the code from both of
you and you have helped me very much in the past like last summer, I
don't know if you remember or not. I am truely greatful for your
knowledge and help...if you could just see the results of what it is I
am writing you would understand what I'm saying...Herferd Wagner is
helpful also. All of your help has led to my answers. As a programmer,
you know that it's hard to see what someone is doing sometimes.
Sometimes even I don't know what I am trying to do until the light comes
on.
Thanks,
Trint

.Net programmer
(e-mail address removed)
 
Back
Top