2

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>
Gragas Incoming
  • 825
  • 1
  • 9
  • 23
  • 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

3 Answers3

2

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,249
  • 6
  • 16
  • 33
  • 1
    why does the function need to accept two parameters? `function (_,txt)` – Gragas Incoming 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
1

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
  • 11
  • 1
1

let b = $(".jsmatchdate").text().split(' ')[0];
$(".jsmatchdate").html(b);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="jsmatchdate">
11-08-2018 12:30 
</div>
csandreas1
  • 2,270
  • 24
  • 42