NSTimeInterval format for POST with JSON objective-c to DateTime .net format

≯℡__Kan透↙ 提交于 2019-12-11 07:54:55

问题


I retrieve the NSTimeInterval as follows:

NSTimeInterval milisecondedDate = ([[NSDate date] timeIntervalSince1970] * 1000);

Which logs this: UNIX Timestamp: "1307710190993.865967"

I only want the value before the dot to send with JSON like this:

"/Date(1307710131826+0200)/"

Any suggestions?

Thanks in advance,


回答1:


Get the current timestamp in milliseconds since 1970 UNIX Timestamp.

NSTimeInterval millisecondedDate = ([[NSDate date] timeIntervalSince1970] * 1000);

Format the milliesecondedDate to a string with no decimals.

NSString* formattedMilliseconds = [NSString stringWithFormat:@"%.0f", millisecondedDate];

Set up the timestamp in DateTime .net format.

NSString *startOfDateFormat = @"/Date(";
NSString *endOfDateFormat = @"+0200)/";
NSString *dateTimeNow = [NSString stringWithFormat:@"%@%@%@",startOfDateFormat, formattedMilliseconds, endOfDateFormat];

There is probably a much better solution for doing this, but this worked for me for now.



来源:https://stackoverflow.com/questions/6306718/nstimeinterval-format-for-post-with-json-objective-c-to-datetime-net-format

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!