hai

i want to know that how to compare the two srting's ... i mean i have a date in string 1 and in the second string i have other date
now i want to compare by operant <,>, i convert that string to int but it through error

Dani AI

Generated

Converting a date string to an int is why the code failed — dates are not plain integers. Both and pointed toward converting the strings to date/time values, which is correct. The next steps are about parsing reliably (formats and culture) and comparing values in a safe, timezone-aware way so results are consistent across inputs.

  • Trim and validate each string first; reject empty or malformed input before attempting conversion.
  • Determine the expected date pattern (dd/MM/yyyy vs MM/dd/yyyy). For fixed formats, parse with a strict parser (TryParseExact) and a known culture to avoid ambiguity.
  • For user-supplied or multiple possible formats, use a safe parse attempt (TryParse) with explicit culture and handle failures instead of throwing exceptions.
  • Compare the parsed values as date/time objects (or DateTimeOffset if time zones matter). Normalize to UTC when comparing moments from different zones.
  • If a numeric representation is required, convert to ticks or a 64-bit epoch value, not a 32-bit int (overflow risk).
  • Log parsing failures and show clear errors so bad inputs can be corrected.

For implementation details and edge cases, see the official docs on TryParseExact for parsing patterns, DateTimeOffset for timezone-aware comparisons, and the standard round-trip format for unambiguous serialization: DateTime.TryParseExact, DateTimeOffset, Round-trip ("O") format specifier.

Recommended Answers

All 2 Replies

Hi there
Check it out this sample code in vb

Private Function compareDate() As String
        Dim d1 As String = "01/01/2009"
        Dim d2 As String = "01/02/2009"
        Dim strmessage As String
        If CType(d1, Date) > CType(d2, Date) Then
            Return strmessage = "greater"
        ElseIf CType(d1, Date) < CType(d2, Date) Then
            Return strmessage = "Lesser"
        End If
    End Function

Mark as solved if it helps you!!!

In C#:

DateTime time1 = DateTime.Parse("09/11/2000");
DateTime time2 = DateTime.Parse("09/12/2000");

if(time1 > time2) {...}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.