在 ionic 3 中仅获取格式为 2018-02-28 的日期 [英] Get only date in format 2018-02-28 in ionic 3

查看:14
本文介绍了在 ionic 3 中仅获取格式为 2018-02-28 的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在最新版本的 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);

这篇关于在 ionic 3 中仅获取格式为 2018-02-28 的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆