0
let date = invoice.due_date;
    console.log(date);

Output 2019-06-13 00:00:00

d = date.split(' ')[0]; //didnt work for me

How can I remove the time and only have the date.

zuyi
  • 319
  • 1
  • 4
  • 13
  • 3
    Is this `invoice.due_date` a string? – JkAlombro Jun 13 '19 at 06:26
  • 1
    "_`d = date.split(' ')[0]; //didnt work for me`_" Is meaning what? That's the way the task is often done. Any error messages in the console? – Teemu Jun 13 '19 at 06:28
  • Possible duplicated thread. Check [this](https://stackoverflow.com/a/3552496/7430022) – wasanga7 Jun 13 '19 at 06:56
  • 1
    We need to know what `didnt work for me` means. If you have a console error saying split is not a function, then that means that invoice.due_date is not a string. It is an object that has a toString method that shows the string you have. If you od NOT have a console error then we need to know what is not working for you – mplungjan Jun 13 '19 at 08:15
  • Sorry it was syntax error its is actually working but its not the most reliable method – zuyi Jun 13 '19 at 11:14

4 Answers4

2

I just added .toLocaleDateString

The toLocaleDateString() method returns a string with a language-sensitive representation of the date portion of the date. The locales and options arguments let applications specify the language whose formatting conventions should be used and allow to customize the behavior of the function.

let date = new Date("2019-06-13T02:00:00Z").toLocaleDateString()
console.log(date)

Reference:

Another Example: If you want to have a ISO Date try this one:

date = new Date('2019-06-13T02:00:00Z');
year = date.getFullYear();
month = date.getMonth() + 1;
dt = date.getDate();

if (dt < 10) {
  dt = '0' + dt;
}
if (month < 10) {
  month = '0' + month;
}

console.log(year + '-' + month + '-' + dt);
momonsterrr
  • 366
  • 2
  • 13
  • If he had a string, the split would work. It is either not a string (e.g. null) or a date. The console.log shows a toString version so it is a date object. DateObject.toISOString() does the trick without formatting manually – mplungjan Jun 13 '19 at 07:02
  • Why is this voted up? on my computer toLocaleString is **6/13/2019** - it is not useful to OP if he wants 2019-06-13. If this is voted up for the formatting, then it is a dupe of [this answer](https://stackoverflow.com/questions/3552461/how-to-format-a-javascript-date) – mplungjan Jun 13 '19 at 07:19
  • 1
    @mplungjan in defence of `toLocaleDateString()`, OP hasn't said at all what they _"want"_. This question is very low quality – Phil Jun 13 '19 at 07:37
  • 1
    @Phil "How can I remove the time and only have the date? I tried `"2019-06-13 00:00:00".split(" ")[0]` - is pretty obvious to me - but yes, it is of low quality because there is no mentioning of WHY that did not work for him. If he meant "It did not work for me because I wanted `6/13/2019` but got `2019-06-13`" then of course I am mistaken – mplungjan Jun 13 '19 at 07:43
-1

If you had a string, the split would work.

It is either not a string (e.g. null) or something else not a string.

Your console.log shows a date string so it is obviously a Date object.

To get the second part in ANY case (space or with a T between the date and time) you need to get the ISOString to be able to PERSISTENTLY get the correct output.

Any toLocaleString or similar is implementation and locale dependent

let date = invoice.due_date.toISOString()

Like this:

// Assuming a date object because your console log and the split that does not work

const invoice = {
  due_date : new Date("2019-06-13 00:00:00") // EXAMPLE date
}  

let date = invoice.due_date.toISOString(); 
console.log(date)
console.log(date.split(/[T| ]/)[0]); // take space or "T" as delimiter
mplungjan
  • 134,906
  • 25
  • 152
  • 209
  • You are passing a string and getting a string back? Why not operate on the initial string? – Gaurav Punjabi Jun 13 '19 at 06:34
  • 3
    Gonna comment on your answer instead of trying to ask on all the duplicate comments... _"He needs the ISO date and there might/will be a T in that"_ how do figure this? I'm just curious because OP has offered next to no information. They certainly haven't specified anything about needing an ISO date – Phil Jun 13 '19 at 06:41
  • 2
    @mplungjan You are making assumptions that what he has is a date object and downvoting everyone, when he has never mentioned that. – Gaurav Punjabi Jun 13 '19 at 06:51
  • Yes because mine would work regardless and everyone are making assumptions here – mplungjan Jun 13 '19 at 06:51
  • 1
    Downvoters: **If he had a string, the split would work. It is either not a string (e.g. null) or a date. The console.log shows a toString version so it is a date object** – mplungjan Jun 13 '19 at 06:58
  • @Phil To CONSISTENTLY parse the date string, he needs the toISOString version of the date - that is what I meant. I will be very surprised if I am wrong – mplungjan Jun 13 '19 at 07:16
  • 1
    Yeah, but as you've pointed out, OP probably already has a `Date` object so why mention _parsing_? FYI, my comments don't relate to your actual answer which I think works pretty nicely, just your comments on the other answers – Phil Jun 13 '19 at 07:20
  • @Phil OP wants the string 2019-06-13 from whatever he has. If he already had a string, his split would work. If he has a DATE object, how do you get the STRING `2019-06-13` from it? In my opinion one SAFE way is to take the toISOString() of the date and split THAT. Alternative is to mess with getFullYear, getMonth+1 getDate and pad them. Makes sense? – mplungjan Jun 13 '19 at 07:25
  • @Phil also the [upvoted answer](https://stackoverflow.com/a/56574611/295783) is mentioning a useless toLocaleString and then does exactly that cumbersome formatting and gets votes for that waste of processing time – mplungjan Jun 13 '19 at 07:28
  • 1
    Only potential problem I see with your solution is that `toISOString()` displays in UTC. It's highly likely OP's original date (wherever that comes from) is probably a local time. Given certain times of the day and OP's timezone, the day part of `toISOString()` might not be what they expect – Phil Jun 13 '19 at 07:33
  • @Phil True. Not known until OP returns – mplungjan Jun 13 '19 at 07:46
-1
let date = invoice.due_date;
console.log(date.getDate() + '-' + (date.getMonth()+1) + '-' + date.getFullYear());

You can try this way. Can create any format like dd-MM-yyyy or anything.

Recommendation: Use moment library for date formatting.

Jignesh M. Khatri
  • 879
  • 1
  • 8
  • 16
-2

You can convert the date string to a Date Object:

let dataObj = new Date(date)

and then format it as given in this link

  • The date he as IS a date object which the problem – mplungjan Jun 13 '19 at 06:31
  • 2
    Did he mention somewhere that it is already a date object? If so, the link I mentioned can be used directly. @mplungjan – Gaurav Punjabi Jun 13 '19 at 06:32
  • If he had a string, the split would work. It is either not a string (e.g. null) or a date. The console.log will show the toString so it is a date object This is really a comment, not an answer. With a bit more rep, [you will be able to post comments](//stackoverflow.com/privileges/comment). – mplungjan Jun 13 '19 at 06:57
  • 2
    @mplungjan I don't even have the required reputation to downvote, so it's not me who has downvoted you. – Gaurav Punjabi Jun 13 '19 at 07:24