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.

jcolebrand
  • 15,889
  • 12
  • 75
  • 121
Justin
  • 17,670
  • 38
  • 132
  • 201
  • How would yo do it in javascript? – Shoban Jan 14 '11 at 02:16
  • Any example of the source would be really useful – dalexsoto Jan 14 '11 at 02: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](https://learn.microsoft.com/en-us/dotnet/api/system.datetimeoffset.fromunixtimeseconds?view=net-5.0#System_DateTimeOffset_FromUnixTimeSeconds_System_Int64_) – StingyJack Dec 19 '20 at 18:44

4 Answers4

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.

Kirk Woll
  • 76,112
  • 22
  • 180
  • 195
Maxim Zaslavsky
  • 17,787
  • 30
  • 107
  • 173
  • 1
    The correct answer. You could use the `AddMilliseconds` method rather than dividing by 1000, although it makes no difference whatsoever in this case – LukeH Jan 14 '11 at 02:33
  • 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 Jan 14 '11 at 02:41
  • @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. – Maxim Zaslavsky Jan 14 '11 at 02:43
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
BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
0

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

sinelaw
  • 16,205
  • 3
  • 49
  • 80
-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?

Tim Carter
  • 590
  • 3
  • 9
  • 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.` (http://msdn.microsoft.com/en-us/library/z2xf7zzk.aspx) Thus, I don't think that this is the correct approach. – Maxim Zaslavsky Jan 14 '11 at 02:48