Convert string to date

0

How do I convert:

var x = "12/10/2017 12:08:26"

pro format:

"Thu Oct 12 2017 00:00:00" 

Does anyone have an idea?

    
asked by anonymous 12.10.2017 / 17:14

3 answers

1

With pure javascript

The constructor new Date () with string must be in the format mm-dd-yyyy or mm/dd/yyyy .

In your case, the date is in Portuguese format dd/mm/yyyy , you need to convert the string to mm/dd/yyyy format.

For this, we use the split () method, which returns an array with the value of each part of the date, using the "/" character as the separator. However, before we separated the date of the time using as separator the space character " "

var x = "12/10/2017 12:08:26";

var partes = x.split(" ");

var horario = partes[1];

var partesData = partes[0].split("/");

var x = partesData[1]+"/"+partesData[0]+"/"+partesData[2];

hoje = new Date(x)
dia = hoje.getDate()
dias = hoje.getDay()
mes = hoje.getMonth()
ano = hoje.getYear()
if (dia < 10)
dia = "0" + dia
if (ano < 1000)
ano+=1900
function CriaArray (n) {
this.length = n }

NomeDia = new CriaArray(7)
NomeDia[0] = "Sun"
NomeDia[1] = "Mon"
NomeDia[2] = "Tues"
NomeDia[3] = "Wed"
NomeDia[4] = "Thurs"
NomeDia[5] = "Fri"
NomeDia[6] = "Sat"

NomeMes = new CriaArray(12)
NomeMes[0] = "Jan"
NomeMes[1] = "Feb"
NomeMes[2] = "Mar"
NomeMes[3] = "Apr"
NomeMes[4] = "May"
NomeMes[5] = "Jun"
NomeMes[6] = "Jul"
NomeMes[7] = "Aug"
NomeMes[8] = "Sep"
NomeMes[9] = "Oct"
NomeMes[10] = "Nov"
NomeMes[11] = "Dec"

console.log( NomeDia[dias] +" " + NomeMes[mes] + " " + dia + " " + ano + " " + horario);
    
12.10.2017 / 20:33
0

Good morning!

Javascript has native handling of dates through the Date class.

You can do something like this:

var date = new Date("12/10/2017 12:08:26");
console.log(date.toString());
    
12.10.2017 / 17:55
0

My idea was to show on the screen in the format:

"12/10/2017 00:00:00" 

But working on the backend in the format

"Thu Oct 12 2017 00:00:00" 

In the case, what I did to solve the problem was to use a moment.js library

link

The final solution looks like this:

var x = new Date(this.$moment(new Date()).format("MM/DD/YYYY HH:mm:ss"))
    
12.10.2017 / 19:09