You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在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#'s DateTime.UtcNow since NSDate doesn't have a timezone attached—it's just a point in time relative to UTC).
  • timeIntervalSince1970 returns 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 09:52:51