'Need help slicing date value

result_database = [
  { id: 1, name: "Tom Riddle", date: "2022-05-16T22:00:00.000Z" },
  { id: 2, name: "Hank Some", date: "2022-05-19T22:00:00.000Z" },
  { id: 3, name: "Family Man", date: "2022-05-17T22:00:00.000Z" },
];
var holiday_date = new Date().toJSON().slice(0, 10);
let holiday_date_remove = result_database.filter(
  (item1) =>
    !result_database.find(
      (item2) =>
        item1.name == item2.name && item2.date.slice(0, 10) == holiday_date
    )
);
const holiday_result = [
  ...new Map(
    holiday_date_remove.map((item) => [JSON.stringify(item.name), item])
  ).values(),
];
console.log(holiday_result);

I'm trying to .slice(0,10) the date. I get it displayed like this "2022-05-16T22:00:00.000Z", but I need it as "2022-05-16", so I try to date.slice(0, 10) it's not working though.



Solution 1:[1]

const dataDate = '2022-05-17T22:00:00.000Z' 

const date = new Date(dataDate)

date.getDate()  + "-" + (date.getMonth()+1) + "-" + date.getFullYear()

console.log(date)

For more information regarding date formats read here

Solution 2:[2]

You should split by "T" when it's in ISOString format.

const yourTypeDate = new Date().toISOString().split('T')[0];

This comparing below is working, it will check if the db date is equal today date, if it's will remove the index, the filter is working.

result_database = [
  { id: 1, name: "Tom Riddle", date: "2022-05-16T22:00:00.000Z" },
  { id: 2, name: "Hank Some", date: "2022-05-19T22:00:00.000Z" },
  { id: 3, name: "Family Man", date: "2022-05-17T22:00:00.000Z" },
];
var holiday_date = new Date().toISOString().split('T')[0];
let holiday_date_remove = result_database.filter(
  (item1) =>
    !result_database.find(
      (item2) => {
        item2.date = item2.date.split('T')[0];
        return ((item1.name == item2.name) && (item2.date.split('T')[0] == holiday_date));
        }
    )
);
const holiday_result = [
  ...new Map(
    holiday_date_remove.map((item) => [JSON.stringify(item.name), item])
  ).values(),
];
console.log(holiday_result);

Solution 3:[3]

In your example, you can use bash shell expansion:

IMAGE_TAG="v0.0.0"
printf "
version: '2.2'
services:
  example-service:
    image: busybox:${IMAGE_TAG:-latest}
    scale: ${REPLICAS:-1}
    command: ls
"

Yields:

version: '2.2'
services:
  example-service:
    image: busybox:v0.0.0
    scale: 1
    command: ls

kubectl (!) supports piping stdin directly (and podman may too), so you can in theory (though it's nearly always better to persist the output to a file so that you can commit a record of what you apply):

IMAGE_TAG="v0.0.0"
printf "
version: '2.2'
services:
  example-service:
    image: busybox:${IMAGE_TAG:-latest}
    scale: ${REPLICAS:-1}
    command: ls
" \
| kubectl apply --filename=-

I have begun using yq to template my YAML files but previous simply used sed. Using the former, you can use a YAML query syntax to path to values that you want to replace and, with sed, of course, regular expressions.

Helm and other (Kubernetes) tools use Go templating to solve a similar problem and it remains something of an outstanding issue (unless you're happy to adopt *nix's philosophy of having tools that do one thing well)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Tyler2P
Solution 2 ricxk
Solution 3