Akash M
Akash M

Reputation: 509

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

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

I want only 2018-02-08.

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

Thank you.

Upvotes: 5

Views: 13736

Answers (5)

Ajantha Bandara
Ajantha Bandara

Reputation: 1521

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 /yarn

npm install moment --save   # npm
yarn add moment             # Yarn
Install-Package Moment.js   # NuGet
spm install moment --save   # spm
meteor add momentjs:moment  # meteor
bower install moment --save # bower (deprecated)

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

Full Documentation : https://momentjs.com/

Upvotes: 2

void
void

Reputation: 913

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

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

Upvotes: 0

Apoorva
Apoorva

Reputation: 1

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

Upvotes: -1

Mohan Gopi
Mohan Gopi

Reputation: 7724

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

Upvotes: 3

Gourav Pokharkar
Gourav Pokharkar

Reputation: 1638

Use Moment.js library.

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

Upvotes: 1

Related Questions