Search code examples
javascriptamazon-web-servicesaws-sdkaws-sdk-js

How can I parse aws api date string?


When I use the aws codecommit api to select one commit I recieve the following json response:

{
  "commit": {
    "additionalData": "",
    "committer": {
      "date": "1505892072 +0200",
      "name": "some name",
      "email": "[email protected]"
    },
    "author": {
      "date": "1505892072 +0200",
      "name": "some name",
      "email": "[email protected]"
    },
    "treeId": "c06c3kr2890sdf80f4e7f1234998cc18c2d672a6",
    "parents": [
      "8jghe808f7f5acc8f067dfg73f88ebfc6e5dfg82"
    ],
    "message": "some message"
  }
}

Now I want to parse the commit date 1505892072 +0200 in javascript. For this the function Date.parse(commtiDate) doesn't work because of the confusing format of the date.

In the example response of the AWS documentation it seems as if the format below is the standard format for api response (code commit api reference).

Have someone an idea how this format works and how to parse it in javascript?


Solution

  • I'm not sure about the +0200 but check the following:

    var seconds = "1505892072";
    var d = new Date(0);
    d.setUTCSeconds(seconds);
    

    Possible timezone adjustments still to be made ;)