DELPHI解析JSON格式化的日期

DELPHI解析JSON格式化的日期json

json返回的日期是 /Date(1560355200000)/ 這樣的格式。函數

這個1560355200000,是指1970年之後的秒數。code

DELPHI如何解析這種日期格式?orm

網上找到的可能是JAVASCRIPT的代碼,不要緊,DELPHI能夠執行JAVASCRIPT函數。blog

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;

  

相關文章
相關標籤/搜索