none
How to get time stamp for the any specific format in c# RRS feed

  • Question

  • Hi

    How to get timestamp in c#  for this result "20181115T13:30:25+0500".

    Please provide the needful solution.

    Thanks in advance.


    Thursday, November 15, 2018 1:24 PM

All replies

  • Please define "timestamp".

    Thursday, November 15, 2018 3:33 PM
  • This is partially an RFC 1123 pattern but the offset isn't correct so you cannot parse it directly as a DateTime. This seems like it is coming from something else like XML or JSON. Please note that deserializers for XML/JSON normally handle this conversion for you so you should simply type the deserialized object correctly to get it to work.

    Given the arbitrary string I think you're going to have to break it up because the standard RFC pattern fails. I tried using this pattern which should work but didn't have any luck.

    var dto = DateTimeOffset.ParseExact(str, "yyyyMMddTHH:mm:ss+/-zzzz", System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat);

    Getting the date is straightforward as you can pick off everything but the offset and then use DateTime.ParseExact. But the offset doesn't really play nice with TimeSpan even for what should be a simple case.

    var re = new Regex(@"(?<date>\d{8}T\d{2}:\d{2}:\d{2})(?<sign>[+-])(?<offset>\d{4})");
    var match = re.Match(str);
    if (match.Success)
    {
        var dtPart = match.Groups["date"].Value;
        var positive = match.Groups["sign"].Value == "+";
        var tzPart = match.Groups["offset"].Value;
                    
        var dt = DateTime.ParseExact(dtPart, "yyyyMMddTHH:mm:ss", System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat);
        var tz = TimeSpan.ParseExact(tzPart, "hhmm", System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat);
    
        if (!positive)
            tz = tz.Negate();
        var dto2 = new DateTimeOffset(dt, tz);
    };   
    There may be a simpler way of doing it.


    Michael Taylor http://www.michaeltaylorp3.net

    Thursday, November 15, 2018 3:42 PM
    Moderator
  • D'oh?

    This is partially an RFC 1123 pattern [..]

    Where does it define a time stamp? It only refers to the RFC 821 time stamp line, which describes the format of the received line added by the MTA at the start of the message.

    Thursday, November 15, 2018 3:49 PM
  • Sure...

    "a digital record of the time of occurrence of a particular event."

    "timestamp is a sequence of characters or encoded information identifying when a certain event occurred, usually giving date and time of day, sometimes accurate to a small fraction of a second."


    Friday, November 16, 2018 6:10 AM
  • timestamp is a sequence of characters or encoded information identifying when a certain event occurred, usually giving date and time of day, sometimes accurate to a small fraction of a second.
    Friday, November 16, 2018 6:10 AM
  • Well, your given string represents a timestamp according to this definition.

    Thus, what is your question about?

    Friday, November 16, 2018 10:36 AM