0

When using this method, my time is returning, the values with month name, time zone and so on.

It's returning as follows: newDate = Mon Sep 02 2019 12:00:00 GMT-0300 (Brasilia Standard Time)

How would I return the format dd / MM / YYYY - hh / MM

sistema.conversao = {
    converterParaDate: function (dataPtbr) {

        //data virá em formato pt-br dd/mm/aaaa hh:mm         
        var arrDataHora = dataPtbr.split(" "); //separar a data dos minutos
        var data = arrDataHora[0];
        var arrHora = [];
        if (arrDataHora.length > 1)
            arrHora = arrDataHora[1].split(":");

        var dia, mes, ano;

        var arrData = data.split("/");
        day= parseInt(arrData[0]);
        mouth= parseInt(arrData[1]) - 1;
        year= parseInt(arrData[2]);


        var hour= 0, minute= 0, second= 0;
        if (arrHora && arrHora.length > 0) {
            hora = arrHora[0];
            minuto = arrHora[1]
        }


        var newDate = new Date(year, mouth, day, hour, minute, second)
        return newDate;

    }

}
Jhensen
  • 71
  • 7

2 Answers2

0

You need to separate every part of the Date object you just created and then rearrange it in any way you need it.

var day = newDate.getDate();
var month = newDate.getMonth();
var year = newDate.getFullYear();
var hours = newDate.getHours();
var minutes = newDate.getMinutes();
var formatDate = day + "/" + month + "/" + year + " - " + hours + "/" + minutes;

return formatDate;

EDIT: Read more about Date methods: https://www.w3schools.com/js/js_date_methods.asp

  • NOTE: Don't forget you already have some of these, so there's no need to write extra code for those. :) – Mauricio Cárdenas Oct 15 '19 at 15:23
  • Please don't reference w3schools, MDN is a better reference as it's a community wikki that is well supported and maintained. – RobG Oct 16 '19 at 07:57
  • I think you mean `... year + " " + hours + ":" + minutes` and they need to be padded to two digits, and the year should be two digits too. – RobG Oct 16 '19 at 08:03
  • @RobG Not to be disrespectful, but that's just nonsense. Both references are good enough for this situation. Also, no, I didn't mean that format, OP specifically requested "dd / MM / YYYY - hh / MM". That's why I put it that way :) – Mauricio Cárdenas Oct 16 '19 at 14:07
  • Now, if OP actually wanted the format you're saying, yeah, I agree. Still, I don't judge his needs, I'm giving him what he's asking for. I'm quite sure OP's smart enough to change the characters to whatever they need. – Mauricio Cárdenas Oct 16 '19 at 14:09
  • And the title wants "dd/MM/YY HH:mm". – RobG Oct 16 '19 at 21:35
-1

Another option would be to use the native JS method toLocaleDateString() to do the formatting. It's easy to change the options to produce different date formatting requirements.

var date = new Date();
var options = {
    day: 'numeric',
    month: 'numeric',
    year: 'numeric',
    hour: 'numeric',
    minute: 'numeric',
    hour12: false
};
var newDate = date.toLocaleDateString('en-US', options).replace(', ', ' - ').replace(':', '/');
console.log(newDate);

Reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleDateString

Mark Hillard
  • 202
  • 3
  • 9
  • That doesn't produce the format the OP wants (it returns something like "10/16/2019, 6:00 PM" whereas the OP wants 16/10/19 18:00). *toLocaleString* is pretty limited in regard to formatting, you essentially have to poke around for a language tag that produces a similar result then munge it from there. It's generally simpler and more reliable to get the parts and arrange them as required. – RobG Oct 16 '19 at 07:59
  • @RobG, my intent was to simply provide another option. Why not use a feature that is native to JavaScript? However, I've updated my answer so that OP's format is produced. – Mark Hillard Oct 18 '19 at 15:36
  • My previous comment was deleted by someone, so I'm adding it back... Thanks for the downvote. – Mark Hillard Oct 23 '19 at 17:03
  • 1
    Not my downvote, I rarely downvote answers. All you need to do is change the value for day, month, hour and minute to "2-digit" and it's OK, though I think the caveat regarding the general usefulness of *toLocaleString* would also be nice (hence the comment). It's OK if the formatting requirements fit the limited options of *toLocaleString*, but go outside that and you'll find a small formatting library (there are plenty of good ones to choose from) is much more helpful and easier to use. – RobG Oct 24 '19 at 00:30
  • E.g. in [Fecha.js](https://github.com/taylorhakes/fecha) it's `fecha.format(new Date(), 'DD / MM / YYYY - hh / mm')`. – RobG Oct 24 '19 at 00:38