1

I have the following div and i want to remove the time e.g. 12:30. What is the best way to do it? The content is dynamic. Here is a Fiddle i have tried

<div class="jsmatchdate">
11-08-2018 12:30 
</div>
  • 1
    Possible duplicate of [How do I remove time part from JavaScript date?](https://stackoverflow.com/questions/34722862/how-do-i-remove-time-part-from-javascript-date) – Amr Elgarhy Aug 04 '18 at 20:20

2 Answers2

1

With JQuery:

   $('.jsmatchdate').text( function (_,txt) {
     return txt.split(' ')[0];
   })

Explanation: we splitted text by ' ' (empty character)
split function returns an array of ['11-08-2018', '12:30'] and we need the first part with index 0.

Basel Issmail
  • 3,847
  • 7
  • 20
  • 36
  • 1
    why does the function need to accept two parameters? `function (_,txt)` –  Aug 05 '18 at 07:14
  • .text( function ) in JQuery documentation: Type: Function( Integer index, String text ) => String, a function returning the text content to set. Receives the index position of the element in the set and the old text value as arguments.... Source: http://api.jquery.com/text/#text-function – Basel Issmail Aug 05 '18 at 07:49
0

if you want to use jQuery then you could try the slice command.

$('.jsmatchdate').text(function (_,txt) {
    return txt.slice(0, 11);
});

What this is doing is getting the text in the div and returning the desired length that you want, i.e. 11-08-2018

Fiddle Link

nDouglass
  • 1
  • 1