2

I'm scraping a website and in the html it has a date in the following format:

"date":"\/Date(1184050800000-0700)\/"

If this was in javascript, it would be a date object and I could use its methods to retrieve the data in whatever format I like. However, I'm scraping it in a C# app. Does anyone know what this format is? Is it the total number of seconds after a certain date or something? I need a way to convert this to a C# datetime object.

3
  • How would yo do it in javascript? Commented Jan 14, 2011 at 2:16
  • Any example of the source would be really useful Commented Jan 14, 2011 at 2:26
  • Anyone coming to this question now should be using the built-in DateTimeOffset.ToUnixTime*(..) and FromUnixTime*(...) and possibly TimeZoneInfo.ConvertTime(..) to handle timezone offset, link to the docs Commented Dec 19, 2020 at 18:44

4 Answers 4

14

If I'm not mistaken, that is a Unix timestamp in milliseconds. 1184050800000 is the timestamp itself, and -0700 is the time zone. This epoch convertor confirms.

Here is some code I've used before for converting Unix timestamps into DateTimes. Be sure to include only the part before -0700:

/// <summary>
/// Converts a Unix timestamp into a System.DateTime
/// </summary>
/// <param name="timestamp">The Unix timestamp in milliseconds to convert, as a double</param>
/// <returns>DateTime obtained through conversion</returns>
public static DateTime ConvertFromUnixTimestamp(double timestamp)
{
    DateTime origin = new DateTime(1970, 1, 1, 0, 0, 0, 0);
    return origin.AddSeconds(timestamp / 1000); // convert from milliseconds to seconds
}

If you encounter Unix timestamps that are in seconds, you just have to remove the / 1000 part of the last line of the code.

Sign up to request clarification or add additional context in comments.

3 Comments

The correct answer. You could use the AddMilliseconds method rather than dividing by 1000, although it makes no difference whatsoever in this case
Does the timestamp take the offset into account, or is the string supposed to mean (timestamp + offset)? My answer uses the offset in its calculation...
@BoltClock I think it's based on UTC time. Thus, after you convert it into a DateTime, you're going to have to express the offset using DateTime functionality.
1

As sinelaw says it seems to be a regex of some sort, however I tried parsing out the numeric values:

1184050800000-0700

And they seem to correspond to:

  • 1184050800000 - Unix timestamp in milliseconds

  • -0700 - this would be the timezone offset UTC-07:00

You could parse it (I assume it's a string from a JSON object) and convert it to a DateTime like this:

string dateString = "/Date(1184050800000-0700)/";
Regex re = new Regex(@"(\d+)([-+]\d{4})");
Match match = re.Match(dateString);

long timestamp = Convert.ToInt64(match.Groups[1].Value);
int offset = Convert.ToInt32(match.Groups[2].Value) / 100;

DateTime date = new DateTime(1970, 1, 1).AddMilliseconds(timestamp).AddHours(-offset);
Console.WriteLine(date); // 7/10/2007 2:00:00 PM

1 Comment

Yes, this seems to be the full solution that OP is looking for. :)
0

Am I wrong? It looks like a regexp to me, not a date object at all.

Comments

-1
DateTime now = new DateTime(1184050800000);
Console.WriteLine(now); // 2/01/0001 8:53:25 AM

Could this be correct if you aren't interested in the year?

1 Comment

I've never used that DateTime constructor before - cool! However, it seems that the parameter means: A date and time expressed in the number of 100-nanosecond intervals that have elapsed since January 1, 0001 at 00:00:00.000 in the Gregorian calendar. (msdn.microsoft.com/en-us/library/z2xf7zzk.aspx) Thus, I don't think that this is the correct approach.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.