仅获取离子3格式2018-02-28的日期 [英] Get only date in format 2018-02-28 in ionic 3
问题描述
我正在使用最新版本的angular中的当前Date()函数.我完全正确.但是我同时看到了日期和时间,如下所示:
I am using current Date() function in latest version of angular. I am getting it perfectly correct. But I am getting both Date and Time as shown below:
today = new Date().toISOString();
console date: 2018-02-08T09:07:15.146
我只想要2018-02-08.
I want only 2018-02-08.
你们能帮助我如何只获取上述格式的日期,而又没有时间.
Can you guys help me how to get only date in above format, without time.
谢谢.
推荐答案
除非您创建自己的函数,否则无法格式化"YYYY-MM-DD" ionic 3这样的日期.但是如果可以使用诸如moment.js之类的库,那就更好了.您可以很容易地使用它来格式化日期和时间.使用npm安装它,然后像波纹管一样导入.
There is no way to format the date like "YYYY-MM-DD" ionic 3 unless you create your own function. But better if you can use a library like moment.js. You can use it very easily to format the date and the time. install it using npm and import like bellow.
import * as moment from "moment";
const date = moment().format("YYYY-MM-DD");
您可以创建自己的函数来返回这样的日期.
You can create your own function to return the date like this.
const date = new Date();
const formatedDate = date.toISOString().substring(0, 10);
这篇关于仅获取离子3格式2018-02-28的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!