From: Tom Shelton on 29 Apr 2010 17:45 On 2010-04-29, Tom Shelton <tom_shelton(a)comcastXXXXXXX.net> wrote: > On 2010-04-29, Saga <antiSpam(a)nowhere.com> 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
From: Saga on 30 Apr 2010 11:52 Thanks again. Saga "Family Tree Mike" <FamilyTreeMike(a)ThisOldHouse.com> wrote in message news:eo2i6R%235KHA.420(a)TK2MSFTNGP02.phx.gbl... > 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
From: Saga on 30 Apr 2010 12:00 Thanks for your reply. You are correct in assuming that the time data is in 24 hour format. Saga "Tom Shelton" <tom_shelton(a)comcastXXXXXXX.net> wrote in message news:%23Lxv8R%235KHA.4940(a)TK2MSFTNGP05.phx.gbl... > On 2010-04-29, Saga <antiSpam(a)nowhere.com> 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
From: Saga on 30 Apr 2010 12:24 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" <FamilyTreeMike(a)ThisOldHouse.com> wrote in message news:eo2i6R%235KHA.420(a)TK2MSFTNGP02.phx.gbl... > 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
First
|
Prev
|
Pages: 1 2 Prev: OLEDB v's Access's FE BE Connection Next: Need help printing multiple pages in VB.Net 2008 |