-1
How do I convert:
var x = "12/10/2017 12:08:26"
pro format:
"Thu Oct 12 2017 00:00:00"
Anyone have any idea?
-1
How do I convert:
var x = "12/10/2017 12:08:26"
pro format:
"Thu Oct 12 2017 00:00:00"
Anyone have any idea?
2
With pure javascript
The new Date() constructor with string must be in 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 the format mm/dd/yyyy
.
For this, we use the split() method, which returns an array with the value of each part of the date, using the "/" character as separator. However, first we separate the date of the time using as separator the character space " "
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);
0
Good morning!
Javascript has native handling of dates through the class Date.
You can do something like this:
var date = new Date("12/10/2017 12:08:26");
console.log(date.toString());
I appreciate your reply, but it did not work no.
What didn’t work? The native toString() method also returns the time zone of the date in question. There are several types of formatting to get the full date. You can have a look at https://www.w3schools.com/js/js_date_formats.asp
I don’t know exactly why, but it was as if toString() didn’t exist. I published the solution that worked down here. Thank you!
Actually, the solution you put on top wasn’t the one that answers your first question. In the question you explain a date format that has the Day of the week and the month in full, thing your solution does not do.
0
My idea was to show on screen in format:
"12/10/2017 00:00:00"
But working on the backend in format
"Thu Oct 12 2017 00:00:00"
In this case, what I did to solve the problem was to use a library Moment.js
The final solution was thus:
var x = new Date(this.$moment(new Date()).format("MM/DD/YYYY HH:mm:ss"))
Browser other questions tagged javascript
You are not signed in. Login or sign up in order to post.
Ever tried something of that question?
– Woss
One of the answers suggested the library I used. Thank you!
– Lucas Augusto