Home > other >  Unix timestamp with only month and year
Unix timestamp with only month and year

Time:12-24

I have an array of different dates but they only include the month and the year. Is there a way to get the unix timestamp just from those two things? I have to iterate over the array to get all the timestamps. I have the solution but the solution is ugly and long. Is there a simpler/easier way to reduce the amount of code I have?

for(var i=0, len = arr.length; i < len; i  ){
        arr[i]['newDate'] = [];
        var jan = day * 31;
        var year = 31556926;
        var day = 86400;
        if(arr[i].post_date.includes("Janurary")){
            //january
            var str = arr[i].post_date.substr(8);
            var current = parseInt(str);
            var yearTotal = (current - 1970) * year;
            var unix = jan   yearTotal;
            var dateString = "@"   unix.toString();
            arr[i].newDate.push(dateString);
        }
        if(arr[i].post_date.includes("February")){
            //february
            var str = arr[i].post_date.substr(8);
            var current = parseInt(str);
            var yearTotal = (current - 1970) * year;
            var unix = feb   yearTotal;
            var dateString = "@"   unix.toString();
            arr[i].newDate.push(dateString);
        }
        if(arr[i].post_date.includes("March")){
            //march
            var str = arr[i].post_date.substr(6);
            var current = parseInt(str);
            var yearTotal = (current - 1970) * year;
            var unix = mar   yearTotal;
            var dateString = "@"   unix.toString();
            arr[i].newDate.push(dateString);
        }
        if(arr[i].post_date.includes("April")){
            //april
            var str = arr[i].post_date.substr(6);
            var current = parseInt(str);
            var yearTotal = (current - 1970) * year;
            var unix = apr   yearTotal;
            var dateString = "@"   unix.toString();
            arr[i].newDate.push(dateString);
        }
        if(arr[i].post_date.includes("May")){
            //may
            var str = arr[i].post_date.substr(4);
            var current = parseInt(str);
            var yearTotal = (current - 1970) * year;
            var unix = may   yearTotal;
            var dateString = "@"   unix.toString();
            arr[i].newDate.push(dateString);
        }
        if(arr[i].post_date.includes("June")){
            //june
            var str = arr[i].post_date.substr(5);
            var current = parseInt(str);
            var yearTotal = (current - 1970) * year;
            var unix = jun   yearTotal;
            var dateString = "@"   unix.toString();
            arr[i].newDate.push(dateString);
        }
        if(arr[i].post_date.includes("July")){
            //july
            var str = arr[i].post_date.substr(5);
            var current = parseInt(str);
            var yearTotal = (current - 1970) * year;
            var unix = jul   yearTotal;
            var dateString = "@"   unix.toString();
            arr[i].newDate.push(dateString);
        }
        if(arr[i].post_date.includes("August")){
            //august
            var str = arr[i].post_date.substr(7);
            var current = parseInt(str);
            var yearTotal = (current - 1970) * year;
            var unix = aug   yearTotal;
            var dateString = "@"   unix.toString();
            arr[i].newDate.push(dateString);
        }
        if(arr[i].post_date.includes("September")){
            //september
            var str = arr[i].post_date.substr(10);
            var current = parseInt(str);
            var yearTotal = (current - 1970) * year;
            var unix = sep   yearTotal;
            var dateString = "@"   unix.toString();
            arr[i].newDate.push(dateString);
        }
        if(arr[i].post_date.includes("October")){
            //october
            var str = arr[i].post_date.substr(8);
            var current = parseInt(str);
            var yearTotal = (current - 1970) * year;
            var unix = oct   yearTotal;
            var dateString = "@"   unix.toString();
            arr[i].newDate.push(dateString);
        }
        if(arr[i].post_date.includes("November")){
            //november
            var str = arr[i].post_date.substr(9);
            var current = parseInt(str);
            var yearTotal = (current - 1970) * year;
            var unix = nov   yearTotal;
            var dateString = "@"   unix.toString();
            arr[i].newDate.push(dateString);
        }
        if(arr[i].post_date.includes("December")){
            //december
            var str = arr[i].post_date.substr(9);
            var current = parseInt(str);
            var yearTotal = (current - 1970) * year;
            var unix = dec   yearTotal;
            var dateString = "@"   unix.toString();
            arr[i].newDate.push(dateString);
        }

        var jan = day * 31;
        var feb = day * 28;
        var mar = day * 31;
        var apr = day * 30;
        var may = day * 31;
        var jun = day * 30;
        var jul = day * 31;
        var aug = day * 31;
        var sep = day * 30;
        var oct = day * 31;
        var nov = day * 30;
        var dec = day * 31; 
    }

my array looks like this

var arr = [
    {
      "post_title": "title",
      "post_date": "December 2021"
    },
    {
      "post_title": "title",
      "post_date": "November 2021"
    },
    {
      "post_title": "title",
      "post_date": "October 2021"
    },
    {
      "post_title": "title",
      "post_date": "September 2021"
    },
    {
      "post_title": "title",
      "post_date": "August 2021"
    },
    {
      "post_title": "title",
      "post_date": "July 2021"
    },
    {
      "post_title": "title",
      "post_date": "June 2021"
    }
]

My result looks like

var arr = [
    {
      "post_title": "title",
      "post_date": "December 2021",
      "newDate": [
        "@1612081626"
      ]
    },
    {
      "post_title": "title",
      "post_date": "November 2021",
      "newDate": [
        "@1611995226"
      ]
    },
    {
      "post_title": "title",
      "post_date": "October 2021",
      "newDate": [
        "@1612081626"
      ]
    },
    {
      "post_title": "title",
      "post_date": "September 2021",
      "newDate": [
        "@1611995226"
      ]
    },
    {
      "post_title": "title",
      "post_date": "August 2021",
      "newDate": [
        "@1612081626"
      ]
    },
    {
      "post_title": "title",
      "post_date": "July 2021",
      "newDate": [
        "@1612081626"
      ]
    },
    {
      "post_title": "title",
      "post_date": "June 2021",
      "newDate": [
        "@1611995226"
      ]
    }
]

CodePudding user response:

You can simply create a new Date() using the month (converted to an index) and year from the string.

Here using an array of months to convert the named month to an index, and dividing the result by 1000 since javascript stores times in milliseconds while unix uses seconds.

const arr = [{ post_title: 'title', post_date: 'December 2021', }, { post_title: 'title', post_date: 'November 2021', }, { post_title: 'title', post_date: 'October 2021', }, { post_title: 'title', post_date: 'September 2021', }, { post_title: 'title', post_date: 'August 2021', }, { post_title: 'title', post_date: 'July 2021', }, { post_title: 'title', post_date: 'June 2021', },];

const months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December',]; 

const result = arr.map((o) => {
  const [month, year] = o.post_date.split(' ');

  return {
    ...o,
    unix_timestamp: new Date(year, months.indexOf(month)).valueOf() / 1000,
  };
});

console.log(result);

Note: months are 0 indexed beginning with 0 for January to 11 for December

CodePudding user response:

You don't really need the lookup table:

const arr = [{ post_title: 'title', post_date: 'December 2021', }, { post_title: 'title', post_date: 'November 2021', }, { post_title: 'title', post_date: 'October 2021', }, { post_title: 'title', post_date: 'September 2021', }, { post_title: 'title', post_date: 'August 2021', }, { post_title: 'title', post_date: 'July 2021', }, { post_title: 'title', post_date: 'June 2021', },];

const result = arr.map(d =>
    ({...d,
    unix_timestamp: new Date(d.post_date.replace(" "," 1. ") " 00:00:00").valueOf() / 1000}));

console.log(result);

  • Related