如何在Objective-C中实现MicrosoftDateFormat日期格式转换?
How to Convert NSDate to MicrosoftDateFormat in Objective-C
To replicate the MicrosoftDateFormat output from your C# code (like "\/Date(1526376504370)\/") in Objective-C, you just need to calculate the UTC timestamp in milliseconds and format it into the required string structure. Here's a complete, working example:
NSDate *currentUTC = [NSDate date]; // Calculate milliseconds since 1970-01-01 UTC long long timestampMilliseconds = (long long)([currentUTC timeIntervalSince1970] * 1000); // Format into the MicrosoftDateFormat string NSString *microsoftDateString = [NSString stringWithFormat:@"\\/Date(%lld)\\/", timestampMilliseconds]; // Verify the output NSLog(@"MicrosoftDateFormat: %@", microsoftDateString);
Breakdown of the steps:
[NSDate date]gives you the current absolute time point (equivalent to C#'sDateTime.UtcNowsince NSDate doesn't have a timezone attached—it's just a point in time relative to UTC).timeIntervalSince1970returns the number of seconds between the date and 1970-01-01 00:00:00 UTC. Multiply by 1000 to get the millisecond timestamp needed for the format.- The string format
@"\\/Date(%lld)\\/"ensures we get the escaped slashes (\/) exactly like the Newtonsoft.Json output from C#. Without the escaped slashes, you'd end up with/Date(...), which won't match the original format.
This will produce output identical to your C# example, ready to use in JSON serialization just like the original app.
内容的提问来源于stack exchange,提问作者Aniket Thakur
相关产品推荐
相关产品推荐

