DELPHI解析JSON格式化的日期


本文整理自网络,侵删。

 

json返回的日期是 /Date(1560355200000)/ 这样的格式。

这个1560355200000,是指1970年以后的秒数。

DELPHI如何解析这种日期格式?

网上找到的多是JAVASCRIPT的代码,没关系,DELPHI可以执行JAVASCRIPT函数。

uses comobj;
 
var js: string=
'function jsondate(jsonDate) {'+
    'try {'+
        'var date = new Date(parseInt(jsonDate.replace("/Date(", "").replace(")/", ""), 10));'+
        'var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;'+
        'var day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();'+
        'var hours = date.getHours();'+
        'var minutes = date.getMinutes();'+
        'var seconds = date.getSeconds();'+
        'var milliseconds = date.getMilliseconds();'+
        'return date.getFullYear() + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds + "." + milliseconds;'+
    '} catch (ex) {'+
        'return "";'+
    '}'+
'}';
 
function RunJs(const JsCode, JsVar: string): string;
var
  script: OleVariant;
begin
  try
    script := CreateOleObject('ScriptControl');
    script.Language := 'JavaScript';
    script.ExecuteStatement(JsCode);
    Result := script.Eval(JsVar);
  except
    Result := '';
  end;
end;
 
 
 
procedure TForm1.Button1Click(Sender: TObject);
begin
  Caption := RunJs(js, Format('jsondate("%s")', ['/Date(1560355200000)/']));
end;

来源:https://www.cnblogs.com/hnxxcxg/p/11040398.html

相关阅读 >>

Delphi winapi: extracticon - 获取 exe、dll 或 ico 文件中的图标

Delphi 如何通过进程句柄判断该进程是否已退出?

Delphi整理一(基础知识)

Delphi textfile读取文本文件

Delphi 获得idhttp redirect后的url

Delphi 正则表达式校验手机号

[译]rad studio 10.4 新变化:面向控件的 vcl 样式管理

Delphi 用文字作为窗体的形状

Delphi �c 如何将多个文件扩展名传递给tdirectory.getfiles?

Delphi datamodule1 fdconnection1数据库连接

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...