Example #1
0
    /// <summary>
    /// Converts the specified string representation of a date and time to its <see cref="DateTime"/> equivalent.
    /// </summary>
    /// <param name="s">A string containing a date and time to convert.</param>
    /// <param name="result">
    /// When this method returns, contains the <see cref="DateTime"/> value equivalent to the date and time
    /// contained in <paramref name="s"/>, expressed as <i>Coordinated Universal Time (UTC)</i>,
    /// if the conversion succeeded, or <see cref="DateTime.MinValue">MinValue</see> if the conversion failed.
    /// The conversion fails if the s parameter is a <b>null</b> reference (Nothing in Visual Basic),
    /// or does not contain a valid string representation of a date and time.
    /// This parameter is passed uninitialized.
    /// </param>
    /// <returns><b>true</b> if the <paramref name="s"/> parameter was converted successfully; otherwise, <b>false</b>.</returns>
    /// <remarks>
    /// The string <paramref name="s"/> is parsed using formatting information in the <see cref="DateTimeFormatInfo.InvariantInfo"/> object.
    /// </remarks>
    public static bool TryParse(string s, out DateTime result)
    {
        //------------------------------------------------------------
        //  Attempt to convert string representation
        //------------------------------------------------------------
        bool wasConverted = false;

        result = DateTime.MinValue;

        if (!String.IsNullOrEmpty(s))
        {
            DateTime parseResult;
            if (DateTime.TryParseExact(Rfc822DateTime.ConvertZoneToLocalDifferential(s), Rfc822DateTime.Rfc822DateTimePatterns, DateTimeFormatInfo.InvariantInfo, DateTimeStyles.AdjustToUniversal, out parseResult))
            {
                result       = DateTime.SpecifyKind(parseResult, DateTimeKind.Utc);
                wasConverted = true;
            }
        }

        return(wasConverted);
    }