0

I have to format a string ("00:10:08:10") into (10 weeks 08 days 10 hrs). So I used a converter and added it with the datagrid binding. My code:

  Public Function Convert(ByVal value As Object, ByVal targetType As System.Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.Convert
    Dim strList(4) As String
    Dim prefixList() As String = {"Wk", "Dy", "Hrs", "Min"}
    Dim input As String = value.ToString
    Dim Output As String = String.Empty
    If input <> String.Empty Then
        strList = input.Split(":")
        Dim itr As Integer
        For Each Str As String In strList
            If Integer.Parse(Str) <> 0 Then
                Output += Str + " " + prefixList(itr) + " "
            End If
            itr += 1
        Next
    End If
    Return Output
End Function

I am getting it converted but when I edit the data, the converter is called again where I wouldn't have the old format("00:10:08:10") and instead its (10 Dy 08 Hr 10 Min). so the converter fails obviously. How can I make it work?

Additional: And is there a data type in mssql to represent my data in days and hours so that i wont have to use this string format.

2
  • Your code might struggle with an input of "00:10:00:20" - and would that be 20 minutes, or 10 weeks to the right? Commented Sep 13, 2010 at 8:01
  • Rowland: why would it struggle? its 20 mins obviously.
    – sarath
    Commented Sep 13, 2010 at 8:23

2 Answers 2

1

You need to implement the ConvertBack function in your ValueConverter to convert from 10 Dy 08 Hr 10 Min to 00:10:08:10.

Assuming the string is in the correct format, it shouldn't be too difficult to parse and convert.

You might consider using a timespan to hold the data. It stores a length of time or duration.

1

About the additional: I always use timestamps (milliseconds since 1/1/1970) when storing times in sql. They are very easy to sort and can be parsed to and from DateTime object (or similar) in almost every programming language.

3
  • But how 'll that suit my requirement? I have to represent the deadline for a particular task. so how can I use a timestamp to reprenst 10 weeks/ 3 days ?
    – sarath
    Commented Sep 13, 2010 at 7:58
  • If your deadline will be static (e.g. meaning a particular due date), you can store that date as a timestamp. Then convert it back into a DateTime and subtract DateTime.Now() from it. This gives you the remaining time. You can also convert DateTime.Now() to timestamp and subtract it from the due date timestamp to get the difference in milliseconds. Commented Sep 13, 2010 at 8:05
  • @geoff - timespan would be the difference between two dates. The term 'timestamp' is from the MySQL shore. Commented Sep 13, 2010 at 8:10

Not the answer you're looking for? Browse other questions tagged or ask your own question.