如何获取当前日期和时间

时间:2021-02-05 14:26:00

标签: javascript angular

需要使用我的时区 UTC-3 获取当前日期和时间,格式为 2021-02-05 12:45:00。这在 Angular 中怎么可能?

我目前使用的 ts

fechaHoy = new Date();

2 个答案:

答案 0 :(得分:0)

我会推荐 dateFormat 模块(moment.js 已弃用)。 首先,按如下方式安装模块 - 当然,在添加未知依赖项之前,始终使用 Git 备份您的工作

npm i dateformat

然后调用:

//Import Module
let dateFormat = require('dateformat');

//Instantiate New Date & then call func and parse date.
let date = new Date();
var currDate = dateFormat(date, "yyyy, dddd, mmmm dS, h:MM:ss TT");

然后您可以使用 currDate 来更新字段等。

答案 1 :(得分:0)

您可以使用此功能

 function formatMyDate(date) {
     function minTwoDigits(n) {
        return (n < 10 ? '0' : '') + n;
    }
 
   return date.getUTCFullYear()+"-"+minTwoDigits(date.getUTCMonth())+"-"+minTwoDigits(date.getUTCDate())+" "+
   minTwoDigits(date.getUTCHours())+":"+ minTwoDigits(date.getUTCMinutes())+":"+  minTwoDigits(date.getUTCSeconds())
 }

用日期对象调用这个函数

formatMyDate(new Date())
//output "2021-01-05 15:03:19"