-1

I try to get today date + time(00:00:00)

So I do it like this:

 const startDate = DateTime.fromISO(params.start)
                  .startOf('day')
                  .toFormat('yyyy-LL-dd HH:mm:ss');

But If I do a console.log():

 console.log('startData',  startDate);

I get an error:

line-chart.service.ts:22 startData Invalid DateTime

So what I have to change?

So I want it to use in here:

return this.sensorNodeService.cameraDataInInterval(
      16,
     startDate,
      '2021-01-20 23:59:59',
      '1'
    );

3 Answers3

3

let c = new Date(); // take the current time

c.setHours(0);
c.setMinutes(0);
c.setSeconds(0);

// prints the current day at 00:00:00 in your timezone
// in the format 2020-12-02 12:10:12
console.log(c.toISOString().replace(/T/, ' ').replace(/\..+/, '')); 

Edit: To format the code in the right way, I used the answer from this question here.

EricHier
  • 446
  • 3
  • 10
0

Here's how you can do it, for both date/time; or time! :)

function DateTime() {
    return (new Date().toLocaleString("en-US", {year: 'numeric',month: 'numeric',day: 'numeric',hour: "numeric",minute: "numeric",second: "numeric",hour12: true}));
}
console.log(DateTime());

function Time() {
    return (new Date().toLocaleString("en-US", {
        hour: "numeric",
        minute: "numeric",
        second: "numeric",
        hour12: true
    }));
}
console.log(Time());
BGPHiJACK
  • 1,277
  • 1
  • 8
  • 16
  • Explain... this is DateTime. – BGPHiJACK Jan 20 '21 at 12:22
  • Right, but the OP's `DateTime` is probably from a library, and asks for help on what they're doing wrong, not on how to implement a such thing... – FZs Jan 20 '21 at 13:02
  • It is a different library, it's luxon.js fast searching would have suggested that within seconds. All answers here offer the native Date-Time feature in JS. What they're doing wrong is not utilizing it. :) – BGPHiJACK Jan 20 '21 at 13:05
0

var todaydate = new Date();
var datetime = todaydate.getDate() + "/" +
  (todaydate.getMonth() + 1) + "/" +
  todaydate.getFullYear() + " " +
  +todaydate.getHours() + ":" +
  todaydate.getMinutes() + ":" +
  todaydate.getSeconds();
  
  
console.log(datetime);
Not A Bot
  • 2,474
  • 2
  • 16
  • 33