typescript 在离子 3 中仅获取格式为 2018-02-28 的日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48682405/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 05:12:51  来源:igfitidea点击:

Get only date in format 2018-02-28 in ionic 3

angulartypescriptionic3

提问by Akash M

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:

我在最新版本的 angular 中使用当前的 Date() 函数。我完全正确。但我同时得到日期和时间,如下所示:

   today = new Date().toISOString();
   console date: 2018-02-08T09:07:15.146

I want only 2018-02-08.

我只想要 2018-02-08。

Can you guys help me how to get only date in above format, without time.

你们能帮我如何在没有时间的情况下只获取上述格式的日期吗?

Thank you.

谢谢你。

采纳答案by Ajantha Bandara

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.

除非您创建自己的函数,否则无法像“YYYY-MM-DD”ionic 3那样格式化日期。但是如果你可以使用像 moment.js 这样的库会更好。您可以非常轻松地使用它来格式化日期和时间。使用 npm 安装它并像下面这样导入。

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);

回答by Mohan Gopi

    var d = new Date().toISOString(); // for now
var joinDate = d.getFullYear() + "-" + (d.getMonth()+1) + "-" + d.getDate();

回答by GeniusGo

Use Moment.jslibrary.

使用Moment.js库。

var today = new Date();
moment(today).format("YYYY-MM-DD");

回答by user11622164

date Argument is the first value which was returning datetime togetherJust Split the string and get the date Only and Do whatever You Want Then

date 参数是第一个一起返回日期时间的值Just Split the string and get the date Only and Do what you want then then

   changeDateFormat2(date){
    const  formated = date.toString().substring(0, 10);
    //let arr = formated.split('-');
    //return `${arr[0]}/${arr[1]}/${arr[2]}`;
  }

回答by Apoorva

today = new Date().toISOString().split(T)[0];