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": "some#email.com"
},
"author": {
"date": "1505892072 +0200",
"name": "some name",
"email": "some#email.com"
},
"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?
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 ;)
Related
I am completely new to the topic of APIs and Postman and have the following question:
How can I detect multiple dates in a string and reformat them into a timestamp?
I pulled a JSON format via an API, then converted it to a string via the JSON.stringify function, and then stored it in an environment variable.
It now looks something like this:
[{“date”:“2000-01-01”,“value”:11.8432},{“date”:“2001-01-01”,“value”:112.2348},{“date”:“2002-01-01”,“value”:182.3777},{“date”:“2003-01-01”,“value”:15.0186},{“date”:“2004-01-01”,“value”:131.3781},{“date”:“2005-01-01”,“value”:145.3683}]
Now I’m trying to get this string back into a JSON format but I’d like to add a UNIX time stamp to the date (in milliseconds since January 1, 1970).
So it should look something like this:
[{“date”:946684800000,“value”:11.8432},{“date”:978307200000,“value”:112.2348},{“date”:1009843200000,“value”:182.3777}…
Does anyone know how to solve this within Postman (JavaScript for Postman)?
use moment library and the valueOf() method:
moment = require('moment')
let date = [{ "date": "2000-01-01", "value": 11.8432 }, { "date": "2001-01-01", "value": 112.2348 }, { "date": "2002-01-01", "value": 182.3777 }, { "date": "2003-01-01", "value": 15.0186 }, { "date": "2004-01-01", "value": 131.3781 }, { "date": "2005-01-01", "value": 145.3683 }]
date = date.map((item) => {
item.date = moment(item.date, "YYYY-MM-DD").valueOf()
return item
});
console.log(date)
created a simple crud application on nodejs and mysql. but have one problem, cant display the date without time and in the proper format.
{
"id": 1,
"name": "Rick Sanchez",
"dob": "1960-05-20T19:00:00.000Z",
"phone": "84351548898",
"comments": "Genius in the galaxy"
},
in the phpmyadmin, the date in the dob i chose as the datetime format.
I believe you should use the moment.js library which is more powerful and has great options(like format).
In your case, you can do the following.
let date = moment('1960-05-20T19:00:00.000Z').utc().format('YYYY-MM-DD');
console.log(date);
You can use toLocaleDateString() method,
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleDateString
const obj = {
"id": 1,
"name": "Rick Sanchez",
"dob": "1960-05-20T19:00:00.000Z",
"phone": "84351548898",
"comments": "Genius in the galaxy"
};
const dateOfBirth = (new Date(obj.dob)).toLocaleDateString();
console.log(dateOfBirth);
You could create a date Object from the string with const myDate = new Date(myString) and then use a method like myDate.toLocaleDateString() to retrieve only the date part
I'm storing data in my local Node JS Elastic Search database, now I've inserted records and one of the fields is a created_at field, which I've stored with new Date(), however, upon retrieval of the data, it seems that these are Strings, and thus my query doesn't seem to return any results.
My data looks like:
{
"_index": "my_table",
"_type": "_doc",
"_id": "V1mouXcBt3ZsPNCwd3A1",
"_version": 1,
"_seq_no": 0,
"_primary_term": 1,
"found": true,
"_source": {
"properties": {
"created_at": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss"
},
"updated_at": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss"
}
},
"data": {
"name": "performance_overview",
"friendly_name": "Performance Overview",
"data": {
"cached": "9:39:21am",
"from": "02/19/2021 00:00:00",
"to": "02/19/2021 23:59:59",
"success": true,
"message": "message",
"sessions": "265",
"leads": "123",
"conversion_rate": "46.42"
},
"created_at": "2021-02-19 09:02:16",
"updated_at": "2021-02-19 09:02:16"
}
}
}
I'm using Moment JS, and have formatted the dates, and attempted what I believe is accurate for field mappings, although I'm new to Elastic Search and this seems to not return any results either.
const from = moment(from).startOf('day')
const results = await elastic.find('my_table', {
query: {
range: {
created_at: {
gte: moment(from).format('YYYY-MM-DD HH:MM:SS'),
}
}
}
})
elastic.find is a custom function that I've written which looks like the following and is exported from another JS file:
const find = async (table, data) => {
try {
const found = await client.search({
index: table,
body: data
}, {
ignore: [404],
maxRetries: 3
})
return found.body
} catch (err) {
return err
}
}
Elasticsearch is a JSON-in, JSON-out interface that stores your datetime data as strings of a given format (could be numeric (milli)seconds since the epoch too, if you choose so.)
As to why new Date() was converted to a string — at some point before the network transport, the JS client will serialize your documents-containing request body by calling JSON.stringify on it. When this function is called on a JS object, the JS runtime looks for an implementation of the .toJSON method on that object and serialize the value returned by the this method. You can verify this by running:
const date = new Date();
console.assert(`"${date.toJSON()}"` === JSON.stringify(date))
Now, in your elastic.find('my_table', {...}) call you're attempting two things at once — setting the mapping and querying the index. That's not going to work.
You've got to define your mapping before you ingest any documents. When you do that, make sure your date fields have a proper format to prevent inconsistencies down the line:
{
"properties": {
"created_at": {
"type": "date",
"format": "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"
}
}
}
After that add some documents to your table/index (through the index operation).
Then and only then will you be able to use your range query. Here's a good working example.
Previously I was using a json file with the following format:
[{"lat":43.788458853157117,"lng":-79.282781549043008,"category":"volunteer","name":"Rita","url":"", "description":"xxx is a member of 13"},{"lat":43.7,"lng":-79.4,"category":"organization","name":"TCAN","url":"http://tcan.ca","description":"Lorem ipsum"}]
Now I am attempting to generate the json file from a Drupal site and am getting the following structure. How can I reference the lowest level fields. I have looked at examples using d3.net but have not found any that apply.
{
"organizations": [
{
"member": {
"field_category": "organization",
"body": "A network of organizations in Toronto devoted to climate change mitigation and adaptation.",
"URL": "xxx.ca",
"title": "Toronto Climate Action Network",
"field_lat": 43.7,
"field_long": -79.4
}
},
{
"member": {
"field_category": "abc",
"body": "xxx.",
"URL": "",
"title": "yyy",
"field_lat": 43.7,
"field_long": -79.28
}
}
]
}
Assuming that your data is stored in the variable data:
var bottom = data.organizations.map(function(d) { return d.member; });
I got a very simple txt file in JSON format:
{
"menu": "File1",
"bday": [
{
"name": "teo",
"date":"22"
},
{
"name": "john",
"date": "9"
},
{
"name": "maria",
"date": "15"
}
]
}
All I want is to just fetch the data and print them. Like:
teo : 22
john:9
...etc...
I don't care about this. I just want to fetch the data.
Your answer is plainly XMLHttpRequest, which is multi-browser compatible JavaScript without need for a library. You can always create your own library for some backward-compatibility.
Put the JSON in a file on your server (for example, data.js) and then use jQuery to fetch the file. Something like this:
var json;
$.getJSON("data.js", function(data){
json = data;
});