2

I have this function to print out dates or minutes/hours from tweets. It works fine but dates are in English. How can I change the months into e.g. Spanish? The date format is fixed - it comes from the Twitter API. It looks like this:

var $time = "Fri May 04 20:33:17 +0000 2018";

function relative_time(time_value) {
      // How can I apply these months to the function?
      //var months = Array('Enero', 'Febrero', 'Marzo', 'Abril', 'Mayo', 'Junio', 'Julio', 'Agosto', 'Septiembre', 'Octubre', 'Noviembre', 'Diciembre');

      var values = time_value.split(" ");
      time_value = values[1] + " " + values[2] + ", " + values[5] + " " + values[3];
      var parsed_date = Date.parse(time_value);
      var relative_to = (arguments.length > 1) ? arguments[1] : new Date();
      var delta = parseInt((relative_to.getTime() - parsed_date) / 1000);
      var shortdate = time_value.substr(4, 2) + " " + time_value.substr(0, 3);
      delta = delta + (relative_to.getTimezoneOffset() * 60);

      if (delta < 60) {
        return '1m';
      } else if (delta < 120) {
        return '1m';
      } else if (delta < (60 * 60)) {
        return (parseInt(delta / 60)).toString() + 'm';
      } else if (delta < (120 * 60)) {
        return '1h';
      } else if (delta < (24 * 60 * 60)) {
        return (parseInt(delta / 3600)).toString() + 'h';
      } else if (delta < (48 * 60 * 60)) {
        //return '1 day';
        return shortdate;
      } else {
        return shortdate;
      }
    }

Check out Fiddle here.

Meek
  • 3,086
  • 9
  • 38
  • 64
  • 1
    Using a library like MomentJS will make your life easier. – 31piy May 08 '18 at 08:58
  • Are the date formats fixed? If yes,what is the output date format and input date format? – Ashish Lohia May 08 '18 at 09:02
  • If your input format is "Fri May 04 20:33:17 +0000 2018" you can easily create a date object by new Date("Fri May 04 20:33:17 +0000 2018") – Ashish Lohia May 08 '18 at 09:03
  • The date format is fixed - it comes from the Twitter API: Like `Fri May 04 20:33:17 +0000 2018` - but how can I change it from printing `May` to `Mayo`? – Meek May 08 '18 at 09:05
  • Possible duplicate of [How to format a JavaScript date](https://stackoverflow.com/questions/3552461/how-to-format-a-javascript-date) – user2226755 May 08 '18 at 09:25

3 Answers3

0
var date = new Date('Fri May 04 20:33:17 +0000 2018');
var months= ['Enero', 'Febrero', 'Marzo', 'Abril', 'Mayo', 'Junio', 'Julio', 'Agosto', 'Septiembre', 'Octubre', 'Noviembre', 'Diciembre'];
var outDate= date.getDate()+ ' '+ months[date.getMonth()];
Ashish Lohia
  • 269
  • 1
  • 12
0

With moment.JS : viernes, 4 de mayo de 2018 22:33

const $time = "Fri May 04 20:33:17 +0000 2018";

const myDate = new Date($time);
const myMoment = moment(myDate);

document.body.innerHTML = myMoment.format('LLLL');
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.19.1/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.1/locale/es.js"></script>
user2226755
  • 12,494
  • 5
  • 50
  • 73
  • I'd rather not use external ressources other than jQuery, but thanks. – Meek May 08 '18 at 09:19
  • @Meek You can write your own code yourself, Why do you wait someone write for you ? https://stackoverflow.com/questions/3552461/how-to-format-a-javascript-date – user2226755 May 08 '18 at 09:25
0
var $time = "Fri May 04 20:33:17 +0000 2018";

function relative_time(time_value) {
    var date = new Date(time_value);
    var months= ['Enero', 'Febrero', 'Marzo', 'Abril', 'Mayo', 'Junio', 'Julio', 
    'Agosto', 'Septiembre', 'Octubre', 'Noviembre', 'Diciembre'];
    return date.getDate()+ ' '+ months[date.getMonth()];
   }

relative_time($time);

Does this answer your question?

Ashish Lohia
  • 269
  • 1
  • 12