locked
Help with Date function RRS feed

  • Question

  • User-73514677 posted

    Hi.

    I have an existing VB script, which I think is trying to check the date is valid or not. I am trying to create a similar code in C#, but not able to understand the code.

    'Date Formats
    CONST DATE_FORMAT_YYYYMMDD = 1
    CONST DATE_FORMAT   = 2
    CONST DATE_FORMAT_UNKOWN = 99
    
    Function SniffDateFormat( szDate )
            
        If IsYYYYMMDD( szDate ) Then
            SniffDateFormat = DATE_FORMAT_YYYYMMDD 
            Exit Function
         End If    
         
         SniffDateFormat = DATE_FORMAT
    
    End Function
    
    
    Function IsYYYYMMDD( szDate )
        If 8 <> Len ( szDate ) Then
            IsYYYYMMDD = False
               Exit Function
        End If
    
        dim yr, mn, dy
        yr = left( szDate, 4 )
        mn = mid( szDate, 5, 2 )
        dy = Right(szDate, 2 )
        
        if (yr < 1900 OR  yr > 3000) then   
            IsYYYYMMDD = False
            Exit Function
        end if
    
        if (mn < 0 OR mn > 12) then 
            IsYYYYMMDD = False
            Exit Function
        End if
        
        if (dy < 0 OR dy > 31) then
            IsYYYYMMDD = False
            Exit Function
        End If
            
        IsYYYYMMDD = True
    
    End Function

    Is there a different way , which can be used for this above functionality?

    Thanks

    Thursday, March 26, 2020 9:31 AM

All replies

  • User-73514677 posted

    Hi.

    I tried the below:

    DateTime date;
                    bool isValidBookedDate = true;
               
                    foreach (DataRow row in excelDataSet.Tables[0].Rows)
                    {
                        if (DateTime.TryParse(row[5].ToString().Trim(), System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, out date))
                        {
                            isValidBookedDate = true;
                        }
                        else
                        {
                            isValidBookedDate = false;
                        }

    Is this the same as the VB Script trying to do ?

    Thanks

    Thursday, March 26, 2020 10:31 AM
  • User475983607 posted

    Is this the same as the VB Script trying to do ?

    No.  Not even close.  The VB code detects if the date is formatted as yyyyMMdd and returns a Boolean.

    Thursday, March 26, 2020 11:42 AM
  • User-73514677 posted

    Thanks.

    But I when I saw the code, the logic seems a bit wrong.

    The existing function YYYYMMDD will consider "20200000" as well as "20200231" a valid date. This seems confusing.

    Is there a better way to do this in C#?

    Thanks

    Thursday, March 26, 2020 12:21 PM
  • User475983607 posted

    venkatzeus

    But I when I saw the code, the logic seems a bit wrong.

    But, that's not what you asked...  

    venkatzeus

    The existing function YYYYMMDD will consider "20200000" as well as "20200231" a valid date. This seems confusing.

    Is there a better way to do this in C#?

    Okay, so you are trying to improve the VB code?  Well, your C# code will not work if the date is formatted as yyyyMMdd.   Again, the VB code is validating a string that could be a date.  What the code does with the date after the validation is unknown to the community. 

    The community can only see the code you share.  At this point, you have not provided enough information for the community to guess what problem you are trying to solve.

    Thursday, March 26, 2020 12:34 PM
  • User-73514677 posted

    Hi.

    The date is send to a insert statement . The column type in SQL is smalldatetime and the some of the existing data in the table looks like : 2019-09-30 00:00:00

    Thanks

    Thursday, March 26, 2020 12:59 PM
  • User475983607 posted

    The date is send to a insert statement . The column type in SQL is smalldatetime and the some of the existing data in the table looks like : 2019-09-30 00:00:00

    Your response is not helpful.

    For the third time, the VB code is very basic.  The VB code checks if a date sting is formatted as yyyyMMdd.  The community does not know the source of the date or what happens to the date after the check.  We can only see the VB code that you shared.

    Your code is seems to be parsing an data from an excel dataset.   At lest that's what the variable name indicates.  Your code expects a standard date format not yyyyMMdd.  Does your code work as expected?  Have you tried running your code?  Is it possible for  the Excel worksheet to contain a date formatted as yyyyMMdd and that's why the VB code checks for yyyyMMdd.  Maybe someone on your team has the application specs?

    Thursday, March 26, 2020 1:20 PM
  • User-73514677 posted

    Hi.

    Thanks for the reply.

    F6 is the row number in the excel sheet, which contains the date field

    The code is used like this:

       if DATE_FORMAT_YYYYMMDD =  SniffDateFormat( f6 ) Then
     f6 = Mid( f6, 5, 2 ) & "/" & Right( f6, 2 ) & "/" & Left( f6 , 4 )
      else
     f6 = Month(f6) & "/" & Day(f6) & "/" & Year(f6)
      end if

    Thursday, March 26, 2020 1:43 PM
  • User475983607 posted

    venkatzeus

    Hi.

    Thanks for the reply.

    F6 is the row number in the excel sheet, which contains the date field

    The code is used like this:

       if DATE_FORMAT_YYYYMMDD =  SniffDateFormat( f6 ) Then
     f6 = Mid( f6, 5, 2 ) & "/" & Right( f6, 2 ) & "/" & Left( f6 , 4 )
      else
     f6 = Month(f6) & "/" & Day(f6) & "/" & Year(f6)
      end if

    The code is very very clear.  If the format is YYYYMMDD then the code use the Mid function to format the date as MM/dd/yyyyy.  Your code does not have this check.  At least, you have not shared any code with that mirrors the check. 

    Perhaps there is someone on your team that can help you understand how the application works?

    Thursday, March 26, 2020 2:07 PM
  • User-73514677 posted

    Thanks for the reply.

    No, i don't have any.

    Should I use the similar way in C# or is there a different better approach available?

    Thanks

    Thursday, March 26, 2020 2:13 PM
  • User475983607 posted

    venkatzeus

    Should I use the similar way in C# or is there a different better approach available?

    The C# substring() method is similar to VB Mid().

    https://docs.microsoft.com/en-us/dotnet/api/system.string.substring?view=netframework-4.8

    VB references

    https://docs.microsoft.com/en-us/office/vba/language/reference/user-interface-help/mid-function.

    https://docs.microsoft.com/en-us/office/vba/language/reference/user-interface-help/year-function

    https://docs.microsoft.com/en-us/office/vba/language/reference/user-interface-help/month-function

    https://docs.microsoft.com/en-us/office/vba/language/reference/user-interface-help/day-function

    You'll need read and learn the VB syntax and go through the code to understand how the code works.  There are also VB to C# converters that are easily found with a simple Google search.

    Thursday, March 26, 2020 2:21 PM
  • User753101303 posted

    Hi,

    DateTime.TryParse should work better than the VB Script code you shown.

    My first move would be to look at which value is found in row[5]. Not 100% sure but if using an Excel driver it might be exposed as a numeric value that needs to be converted using the https://docs.microsoft.com/en-us/dotnet/api/system.datetime.fromoadate?view=netframework-4.8 function.

    ie rather than posting code that doesn't work, do take few extra minutes to see what happens and so here looking at row[5] to understand why DateTime.TryParse doesn't do what you expect.

    Thursday, March 26, 2020 2:56 PM
  • User1535942433 posted

    Hi venkatzeus,

    Accroding to your description,I suggest you could use DateTime.TryParseExact.It will return bool type.

    More details,you could refer to below codes:

    string a = "20200327";
                string b = "20236985";
                DateTime startDate;
                var dt = DateTime.TryParseExact(a, "yyyyMMdd", System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, out startDate);
                Response.Write(dt);

    Best regard,

    Yijing Sun

    Friday, March 27, 2020 8:17 AM