How to convert a string to Local Date?

0

Goodbye people, I have dates in TD elements of a table that I want to modify, the time of those TDs is 11 hours +, and I have to transform them to local time and I also want to add the name of the DAY.

As these TDs have a fixed width, the time is lower and so I want it to be because the height of the row allows it well, that is, it looks like this:

10/18/20
 8:30 PM

The HTML of the TDs is like this:

<td class="texto-cen">18/10/20 20:30</td>

And there should be something like this:

<td class="texto-cen">20 Oct 18 Sab 09:30</td>

And it should look like this:

20 Oct 18
Sat 09:30

You could also create another element within this TD to only set the DAY and TIME if there is not a date format that is exactly: 20 Oct 18 Sat 09:30. For example:

<td class="texto-cen">20 Oct 18 <div>Sab 09:30</div></td>

Well then I should give one format or another to each cell depending on the DAY NAME, but this I think I can solve it but I'll comment on it if someone has a code already done.

Well as you know, there are many Javascript functions to do it but I am confused to say it in some way and I can not find the way.

Greetings and Thanks for your help!

    
asked by Daniel Martinez 01.11.2018 в 23:23
source

2 answers

0

I think the easiest and fastest way to do it is with Moment.js if you can include it in your project. I show you an example of how it would look:

moment.locale('es', {
  months: 'Enero_Febrero_Marzo_Abril_Mayo_Junio_Julio_Agosto_Septiembre_Octubre_Noviembre_Diciembre'.split('_'),
  monthsShort: 'Enero._Feb._Mar_Abr._May_Jun_Jul._Ago_Sept._Oct._Nov._Dec.'.split('_'),
  weekdays: 'Domingo_Lunes_Martes_Miercoles_Jueves_Viernes_Sabado'.split('_'),
  weekdaysShort: 'Dom._Lun._Mar._Mier._Jue._Vier._Sab.'.split('_'),
  weekdaysMin: 'Do_Lu_Ma_Mi_Ju_Vi_Sa'.split('_')
});

var p = document.querySelector('#p');
p.innerHTML = moment().format('DD  MMM YY') + "<br>" + moment().format('ddd HH:mm');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>
<!DOCTYPE html>
<html lang="en">
<head>
  <title>Document</title>
</head>
<body>
  <p id="p"></p>
</body>
</html>
    
answered by 02.11.2018 в 03:23
0

I think it's easier to use <br /> for the line and Date.toLocaleDateString to format the date. Moment.js is not necessary for this solution.

For example:

let texto = "18/10/20 09:30"
let partes = texto.split(" ")
let x = new Date("20"+partes[0].replace(/\//g, '-'))
let tiempo = partes[1].split(":")
x.setHours(tiempo[0])
x.setMinutes(tiempo[1])
let opciones_fecha = { year: 'numeric', month: 'short', day: 'numeric' }
let fecha = x.toLocaleDateString('es-ES', opciones_fecha)
let opciones_hora = { weekday: 'short', hour: "2-digit", minute: "2-digit" }
let hora = x.toLocaleDateString('es-ES', opciones_hora)
document.getElementById("texto-cen").innerHTML = fecha + "<br />" + hora
<span id="texto-cen">texto aquí</span>
    
answered by 02.11.2018 в 04:52