Hi All,

Quick one. I have a webservice which uses numbers in american format which need to be passed as datetime data types when webmethod is called. The issue is the times passed depend on when a button is pressed.

Please ignore variables as this is from testbed project I just use for testing code. Essentially the starttime variable is the time the user clicks the button, starttime is a string. The variable endtime is dependant on the time of day, if between 06:30 and 18:30 endtime is 18:30 that day (the end of the shift), if it is between 18:30 and 06:30 end time is 06:30 (end of night shift).

Essentially I need to get a datetime data type in the format MM/dd/yyyy hh:mm:ss for the corrrect endtime.

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        starttime = "#" & String.Format("{0:MM/dd/yyyy hh:mm:ss tt}", System.DateTime.Now) & "#"
        If TimeOfDay > #6:30:00 AM# Then
            If TimeOfDay < #6:30:00 PM# Then
                endtime = "#" & String.Format("{0:MM/dd/yyyy}", System.DateTime.Now) & " " & "06:30:00 PM#"
            Else
                endtime = "#" & String.Format("{0:MM/dd/yyyy}", System.DateTime.Now) & " " & "06:30:00 AM#"
            End If
        End If
        Label1.Text = starttime
        Label2.Text = endtime
    End Sub

Any ideas?

Cheers
Will

Hi all,

I'm an idiot, just figured part of it out. I have now got it to add the day by using the AddDays facility, code below for everyone:

Public Class Form1
    Dim starttime As String
    Dim endtime As String
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        starttime = "#" & String.Format("{0:MM/dd/yyyy hh:mm:ss tt}", System.DateTime.Now) & "#"
        If TimeOfDay > #6:30:00 AM# Then
            If TimeOfDay < #6:30:00 PM# Then
                endtime = "#" & String.Format("{0:MM/dd/yyyy}", System.DateTime.Now) & " " & "06:30:00 PM#"
            ElseIf TimeOfDay < #11:59:00 PM# Then
                endtime = "#" & String.Format("{0:MM/dd/yyyy}", System.DateTime.Now.AddDays(1)) & " " & "06:30:00 AM#"
            Else
                endtime = "#" & String.Format("{0:MM/dd/yyyy}", System.DateTime.Now) & " " & "06:30:00 AM#"
            End If
        End If
        Label1.Text = starttime
        Label2.Text = endtime
    End Sub
End Class

The issue is now how to I convert this string into a datetime data type?

Cheers
Will

    Dim stime As DateTime = DateTime.Parse(starttime)
    Dim etime As DateTime = DateTime.Parse(endtime)

Since you're controlling the format of the string already I suggest using the Parse method. If you're unsure of the format I suggest using the TryParse method.

Hi All,

I have tried that and the issue is it is saying in the wrong format. I believe that this is because the webservice that is providing the data wants american date/time and as I am from UK the vb.net program is dealing with UK formats so the string is in the wrong format for the date/time variable however is for the webservice.

Is there a way to declare a date/time variable that is in american format for those 2 variables? Wouldnt want to do it for the whole program as the rest is required to work in UK date/time format.

Cheers
Will

A Datetime variable holds no culture specific information. You can however create a string representation for a specific culture.

I'm a bit lost as to whether you are trying to convert a string value from your service or convert to string value to send to your service.

Here are some examples:

   Dim US As New Globalization.CultureInfo("en-US")
   Dim GB As New Globalization.CultureInfo("en-GB")

   Dim dt1 As DateTime = DateTime.Parse("Thursday, January 24, 2013", US)
   Dim dt2 As DateTime = DateTime.Parse("Thursday, January 24, 2013", GB)
   MsgBox("dt1 = dt2: " & (dt1 = dt2).ToString)

   dt1 = DateTime.Parse("1/24/2013", US)
   dt2 = DateTime.Parse("24.01.2013", GB)
   MsgBox("dt1 = dt2: " & (dt1 = dt2).ToString)

   ' format ref: http://msdn.microsoft.com/en-us/library/az4se3k1%28v=VS.71%29.aspx
   Dim strdt1v1 As String = dt1.ToString(format:="D", provider:=US)
   Dim strdt1v2 As String = dt1.ToString(format:="D", provider:=GB)

   dt1 = Now() ' give a time component
   'DateTimeFormatInfo.RFC1123Pattern - this the same format for all cultures
   ' http://msdn.microsoft.com/en-us/library/system.globalization.datetimeformatinfo.rfc1123pattern%28v=vs.90%29.aspx
   ' (strdt1Internetv1=strdt1Internetv2) = true
   Dim strdt1Internetv1 As String = dt1.ToString(format:="R", provider:=US)
   Dim strdt1Internetv2 As String = dt1.ToString(format:="R", provider:=GB)

   Dim strdt1Internetv3 As String = dt1.ToString(format:=US.DateTimeFormat.RFC1123Pattern)
   Dim strdt1Internetv4 As String = dt1.ToString(format:=GB.DateTimeFormat.RFC1123Pattern)
   Dim strdt1Internetv5 As String = dt1.ToString(format:=Threading.Thread.CurrentThread.CurrentCulture.DateTimeFormat.RFC1123Pattern)

I don't think there is a difference in the formats, atleast I couldn't find one. Most likely it's expecting a UTC type or even an ISO type. If it's a database then it probably wants yyyy/mm/dd or something similar. You'll have to contact the webservice's support team and see if you can get that info.

Hi,
Unfortunately the person who designed the web service is no longer about.
I have done query testing and it works when passed datetime as # MM/dd/yyyy hh:mm AM/PM # in a date time variable but not in the UK # dd/mm/yyyy hh:mm # format?
Cheers
Will

Hi TnTinMN,
Sorry missed your reply. For clarity I sent the web service 2 date/time variables starttime and endtime. It then returns a dataset with some info in that I manipulate.
I found it odd, bearing in mind WS is UK programmed that it won't work unless the datetime is in the US format. I am assuming it is to so with the database the WS pulls from being in US format maybe.
I will try your method tomorrow and see how it goes, my main issue is the WS is on a closed network so I have to be at clients site, 2hr drive away, to test anything.
I have however spotted one thing, when I try to Parse the string to date/time I am passing # 01/25/2013 12:22 AM # should I drop the # when trying to parse?
Cheers
Will

There is no need to drop the hash marks. The parser accepts them as delimiting the start and end of datetime value. They are also used to hardcode datetime types.
Dim sometime As DateTime = #2/2/3003 1:00:00 PM#

It could be that the other programmer used MS's InvariantCulture when coding the service. It is just that this culture's DateTimeFormatInfo is identical (not verified) to the US culture's DateTimeFormatInfo property.

Ok no worries, I will give the method involving globalisation a go later and see how that works. I have done the hard coding before and it works fine, that's how I discovered that it would only work if provided with an American format for date time as when I passed it an English format it was returning nil results.
Cheers
Will

This article has been dead for over six months. Start a new discussion instead.