Newtonsoft.Json.JsonReader.ReadAsDateTime C# (CSharp) Method

ReadAsDateTime() public method

Reads the next JSON token from the stream as a Nullable{T} of DateTime.
public ReadAsDateTime ( ) : DateTime?
return DateTime?
        public virtual DateTime? ReadAsDateTime()
        {
            switch (GetContentToken())
            {
                case JsonToken.None:
                case JsonToken.Null:
                case JsonToken.EndArray:
                    return null;
                case JsonToken.Date:
#if !NET20
                    if (Value is DateTimeOffset)
                    {
                        SetToken(JsonToken.Date, ((DateTimeOffset)Value).DateTime, false);
                    }
#endif

                    return (DateTime)Value;
                case JsonToken.String:
                    string s = (string)Value;
                    return ReadDateTimeString(s);
            }

            throw JsonReaderException.Create(this, "Error reading date. Unexpected token: {0}.".FormatWith(CultureInfo.InvariantCulture, TokenType));
        }

Usage Example

        /// <summary>
        /// Reads the next JSON token from the underlying <see cref="JsonReader"/> as a <see cref="Nullable{T}"/> of <see cref="DateTime"/>.
        /// </summary>
        /// <returns>A <see cref="Nullable{T}"/> of <see cref="DateTime"/>. This method will return <c>null</c> at the end of an array.</returns>
        public override DateTime?ReadAsDateTime()
        {
            DateTime?dateTime = _reader.ReadAsDateTime();

            ValidateCurrentToken();
            return(dateTime);
        }
All Usage Examples Of Newtonsoft.Json.JsonReader::ReadAsDateTime