PC Review


Reply
Thread Tools Rate Thread

Adding time to date

 
 
Saga
Guest
Posts: n/a
 
      29th Apr 2010
I need to add the time a a date, specifically, the current date.
This seemed easy enough, but I found that thi wasn't so.

As in input I get 3 or 4 digits representing the date. Valid
input string are:

"0344", "1102", "456"

The minutes are always 2 digits, but the hour can be either
1 or 2 digits.

What I did was first come up with a function to normalize
the input string which formats it hh:mm:

private function sSetTime(byval strTime as string) as integer

strTime = strTime.ToString("D4")

Return strTime.Substring(0, 2) & ":" & strTime.Substring(2, 2)

end function

Next is my date variable where I am going to keep the date
and time:

dim dteEnd as date = date.today()
dim strT as string = "345" 'For example
Now I add the time:

dteEnd = dteEnd.AddHours(
Convert.ToDouble(sSetTime(strT.Split(":".ToCharArray()).GetValue(0).ToString()))
)

And add the minutes

dteEnd = dteEnd.AddMinutes(
Convert.ToDouble(sSetTime(strT.Split(":".ToCharArray()).GetValue(1).ToString()))
)

Is there a better way to do this? <g>
Thanks, Saga


 
Reply With Quote
 
 
 
 
Family Tree Mike
Guest
Posts: n/a
 
      29th Apr 2010
On 4/29/2010 4:44 PM, Saga wrote:
> I need to add the time a a date, specifically, the current date.
> This seemed easy enough, but I found that thi wasn't so.
>
> As in input I get 3 or 4 digits representing the date. Valid
> input string are:
>
> "0344", "1102", "456"
>
> The minutes are always 2 digits, but the hour can be either
> 1 or 2 digits.
>
> What I did was first come up with a function to normalize
> the input string which formats it hh:mm:
>
> private function sSetTime(byval strTime as string) as integer
>
> strTime = strTime.ToString("D4")
>
> Return strTime.Substring(0, 2)& ":"& strTime.Substring(2, 2)
>
> end function
>
> Next is my date variable where I am going to keep the date
> and time:
>
> dim dteEnd as date = date.today()
> dim strT as string = "345" 'For example
> Now I add the time:
>
> dteEnd = dteEnd.AddHours(
> Convert.ToDouble(sSetTime(strT.Split(":".ToCharArray()).GetValue(0).ToString()))
> )
>
> And add the minutes
>
> dteEnd = dteEnd.AddMinutes(
> Convert.ToDouble(sSetTime(strT.Split(":".ToCharArray()).GetValue(1).ToString()))
> )
>
> Is there a better way to do this?<g>
> Thanks, Saga
>
>


"Better" is always debatable... Here is an example of a different approach:

Sub Main()
Dim s() As String = {"0324", "1234", "832"}
Dim hhmm As Integer

For Each s1 As String In s
hhmm = Integer.Parse(s1)

Dim h As Integer = hhmm \ 100
Dim m As Integer = hhmm Mod 100

Dim t As DateTime = DateTime.Today
t = t.AddHours(h).AddMinutes(m)
Console.WriteLine(t)
Next

Console.ReadKey()
End Sub

--
Mike
 
Reply With Quote
 
Saga
Guest
Posts: n/a
 
      29th Apr 2010
Thanks! This looks like a good approach. I can build a function
that will return the date with the time added and use that.
A question regarding your code. I noticed that you used

> hhmm = Integer.Parse(s1)


I typically use CInt(). Is there any advantage to using the
integer parse method or disadvantage to uing cint()? Thanks
Saga

"Family Tree Mike" <(E-Mail Removed)> wrote in message
news:eZlALC%(E-Mail Removed)...
> On 4/29/2010 4:44 PM, Saga wrote:
>> I need to add the time a a date, specifically, the current date.
>> This seemed easy enough, but I found that thi wasn't so.
>>
>> As in input I get 3 or 4 digits representing the date. Valid
>> input string are:
>>
>> "0344", "1102", "456"
>>
>> The minutes are always 2 digits, but the hour can be either
>> 1 or 2 digits.
>>
>> What I did was first come up with a function to normalize
>> the input string which formats it hh:mm:
>>
>> private function sSetTime(byval strTime as string) as integer
>>
>> strTime = strTime.ToString("D4")
>>
>> Return strTime.Substring(0, 2)& ":"& strTime.Substring(2, 2)
>>
>> end function
>>
>> Next is my date variable where I am going to keep the date
>> and time:
>>
>> dim dteEnd as date = date.today()
>> dim strT as string = "345" 'For example
>> Now I add the time:
>>
>> dteEnd = dteEnd.AddHours(
>>
>> Convert.ToDouble(sSetTime(strT.Split(":".ToCharArray()).GetValue(0).ToString()))
>> )
>>
>> And add the minutes
>>
>> dteEnd = dteEnd.AddMinutes(
>>
>> Convert.ToDouble(sSetTime(strT.Split(":".ToCharArray()).GetValue(1).ToString()))
>> )
>>
>> Is there a better way to do this?<g>
>> Thanks, Saga
>>
>>

>
> "Better" is always debatable... Here is an example of a different
> approach:
>
> Sub Main()
> Dim s() As String = {"0324", "1234", "832"}
> Dim hhmm As Integer
>
> For Each s1 As String In s
> hhmm = Integer.Parse(s1)
>
> Dim h As Integer = hhmm \ 100
> Dim m As Integer = hhmm Mod 100
>
> Dim t As DateTime = DateTime.Today
> t = t.AddHours(h).AddMinutes(m)
> Console.WriteLine(t)
> Next
>
> Console.ReadKey()
> End Sub
>
> --
> Mike



 
Reply With Quote
 
Family Tree Mike
Guest
Posts: n/a
 
      29th Apr 2010
On 4/29/2010 5:25 PM, Saga wrote:
> Thanks! This looks like a good approach. I can build a function
> that will return the date with the time added and use that.
> A question regarding your code. I noticed that you used
>
>> hhmm = Integer.Parse(s1)

>
> I typically use CInt(). Is there any advantage to using the
> integer parse method or disadvantage to uing cint()? Thanks
> Saga
>


Just a personal preference, I believe. That said, I wrote that reply
quickly, and the following way would be better:

if (Integer.TryParse(s1, hhmm)) then
' do the datetime stuff
end if

TryParse returns true if the value in the string can be parsed as an
integer. This would handle if the string "Noon" was passed in,
returning false, so that it is skipped.

--
Mike
 
Reply With Quote
 
Tom Shelton
Guest
Posts: n/a
 
      29th Apr 2010
On 2010-04-29, Saga <(E-Mail Removed)> wrote:
> I need to add the time a a date, specifically, the current date.
> This seemed easy enough, but I found that thi wasn't so.
>
> As in input I get 3 or 4 digits representing the date. Valid
> input string are:
>
> "0344", "1102", "456"
>
> The minutes are always 2 digits, but the hour can be either
> 1 or 2 digits.
>
> What I did was first come up with a function to normalize
> the input string which formats it hh:mm:
>


If you don't use a 24 hour clock here (HHmm), then your not ever going to be
able to distinguish between am and pm with out more information . So, here
is what I have assuming 24 hour clock...

Option Explicit On
Option Strict On

Module Module1

Sub Main()
Dim str() As String = {"1146", "0344", "442", "601", "1302", "2359"}

Dim fmt As String = "MM/dd/yyyy HHmm"
For Each t As String In str
Dim cur As String = Date.Today.ToString("MM/dd/yyyy ") & String.Format("{0,4}", t).Replace(" ", "0")
Dim d As Date
Date.TryParseExact(cur, fmt, Nothing, Globalization.DateTimeStyles.None, d)
Console.WriteLine(d)
Next
End Sub

End Module

--
Tom Shelton
 
Reply With Quote
 
Tom Shelton
Guest
Posts: n/a
 
      29th Apr 2010
On 2010-04-29, Tom Shelton <(E-Mail Removed)> wrote:
> On 2010-04-29, Saga <(E-Mail Removed)> wrote:
>> I need to add the time a a date, specifically, the current date.
>> This seemed easy enough, but I found that thi wasn't so.
>>
>> As in input I get 3 or 4 digits representing the date. Valid
>> input string are:
>>
>> "0344", "1102", "456"
>>
>> The minutes are always 2 digits, but the hour can be either
>> 1 or 2 digits.
>>
>> What I did was first come up with a function to normalize
>> the input string which formats it hh:mm:
>>

>
> If you don't use a 24 hour clock here (HHmm), then your not ever going to be
> able to distinguish between am and pm with out more information . So, here
> is what I have assuming 24 hour clock...
>
> Option Explicit On
> Option Strict On
>
> Module Module1
>
> Sub Main()
> Dim str() As String = {"1146", "0344", "442", "601", "1302", "2359"}
>
> Dim fmt As String = "MM/dd/yyyy HHmm"
> For Each t As String In str
> Dim cur As String = Date.Today.ToString("MM/dd/yyyy ") & String.Format("{0,4}", t).Replace(" ", "0")
> Dim d As Date
> Date.TryParseExact(cur, fmt, Nothing, Globalization.DateTimeStyles.None, d)
> Console.WriteLine(d)
> Next
> End Sub
>
> End Module
>


Technically - you should be checking the return value of TryParseExact. It
returns a Boolean value indicating if it was able to succesfully parse the
string as a date or not

--
Tom Shelton
 
Reply With Quote
 
Saga
Guest
Posts: n/a
 
      30th Apr 2010
Thanks again. Saga

"Family Tree Mike" <(E-Mail Removed)> wrote in message
news:eo2i6R%(E-Mail Removed)...
> On 4/29/2010 5:25 PM, Saga wrote:
>> Thanks! This looks like a good approach. I can build a function
>> that will return the date with the time added and use that.
>> A question regarding your code. I noticed that you used
>>
>>> hhmm = Integer.Parse(s1)

>>
>> I typically use CInt(). Is there any advantage to using the
>> integer parse method or disadvantage to uing cint()? Thanks
>> Saga
>>

>
> Just a personal preference, I believe. That said, I wrote that reply
> quickly, and the following way would be better:
>
> if (Integer.TryParse(s1, hhmm)) then
> ' do the datetime stuff
> end if
>
> TryParse returns true if the value in the string can be parsed as an
> integer. This would handle if the string "Noon" was passed in, returning
> false, so that it is skipped.
>
> --
> Mike



 
Reply With Quote
 
Saga
Guest
Posts: n/a
 
      30th Apr 2010
Thanks for your reply. You are correct in assuming that the
time data is in 24 hour format. Saga


"Tom Shelton" <(E-Mail Removed)> wrote in message
news:%23Lxv8R%(E-Mail Removed)...
> On 2010-04-29, Saga <(E-Mail Removed)> wrote:
>> I need to add the time a a date, specifically, the current date.
>> This seemed easy enough, but I found that thi wasn't so.
>>
>> As in input I get 3 or 4 digits representing the date. Valid
>> input string are:
>>
>> "0344", "1102", "456"
>>
>> The minutes are always 2 digits, but the hour can be either
>> 1 or 2 digits.
>>
>> What I did was first come up with a function to normalize
>> the input string which formats it hh:mm:
>>

>
> If you don't use a 24 hour clock here (HHmm), then your not ever going to
> be
> able to distinguish between am and pm with out more information . So,
> here
> is what I have assuming 24 hour clock...
>
> Option Explicit On
> Option Strict On
>
> Module Module1
>
> Sub Main()
> Dim str() As String = {"1146", "0344", "442", "601", "1302",
> "2359"}
>
> Dim fmt As String = "MM/dd/yyyy HHmm"
> For Each t As String In str
> Dim cur As String = Date.Today.ToString("MM/dd/yyyy ") &
> String.Format("{0,4}", t).Replace(" ", "0")
> Dim d As Date
> Date.TryParseExact(cur, fmt, Nothing,
> Globalization.DateTimeStyles.None, d)
> Console.WriteLine(d)
> Next
> End Sub
>
> End Module
>
> --
> Tom Shelton



 
Reply With Quote
 
Saga
Guest
Posts: n/a
 
      30th Apr 2010
Thanks for the addtional tip. In theory, all data has been
validated by the time it reaches this point, but I'll add this
extra validation step just to be on the safe side. Saga


"Family Tree Mike" <(E-Mail Removed)> wrote in message
news:eo2i6R%(E-Mail Removed)...
> On 4/29/2010 5:25 PM, Saga wrote:
>> Thanks! This looks like a good approach. I can build a function
>> that will return the date with the time added and use that.
>> A question regarding your code. I noticed that you used
>>
>>> hhmm = Integer.Parse(s1)

>>
>> I typically use CInt(). Is there any advantage to using the
>> integer parse method or disadvantage to uing cint()? Thanks
>> Saga
>>

>
> Just a personal preference, I believe. That said, I wrote that reply
> quickly, and the following way would be better:
>
> if (Integer.TryParse(s1, hhmm)) then
> ' do the datetime stuff
> end if
>
> TryParse returns true if the value in the string can be parsed as an
> integer. This would handle if the string "Noon" was passed in, returning
> false, so that it is skipped.
>
> --
> Mike



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Adding Time to a Date Ludo Microsoft Excel Programming 1 14th Feb 2008 05:19 PM
Adding Date and Time? netasp Microsoft Excel Discussion 3 10th Aug 2006 10:39 PM
Adding Date/Time fields teastman Microsoft Excel New Users 6 1st Jan 2006 05:08 AM
Adding time to date-time formatted cell =?Utf-8?B?dGF3dHJleShyZW1vdmUgdGhpcyAgKUBwYWNpZmlj Microsoft Excel Misc 4 12th Aug 2005 10:53 PM
Adding a date and time =?Utf-8?B?U2tpcDR0NA==?= Microsoft Excel Worksheet Functions 1 5th Mar 2005 05:37 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:55 AM.