Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

135
Views
remove timezone in my random date generator in javascript

This is my code. I make it random but the timezone is always in there and i dont know how to disappear the timezone, can someone help me with this I'am beginner in this thanks

var startDate = new Date("1990-01-01"); //YYYY-MM-DD
var endDate = new Date("2022-01-01"); //YYYY-MM-DD

function formatDate(date) {
  const year = date.getFullYear();
  /* getMonth returns dates from 0, so add one */
  const month = date.getMonth() + 1;
  const day = date.getDate();
  
  return `${year}-${month < 10 ? '0' : ''}${ month }-${ day < 10 ? '0' : '' }${day}`
}

var getDateArray = function(start, end) {

  return new Date(
    start.getTime() + Math.random() * (end.getTime(0) - start.getTime(0))
  );

}
var dateArr = getDateArray(startDate, endDate);


console.log(dateArr);

about 4 years ago · Santiago Gelvez
2 answers
Answer question

0

If you call the formatDate function you have in your code, I believe that will get rid of the timezone information.

var startDate = new Date("1990-01-01"); //YYYY-MM-DD
var endDate = new Date("2022-01-01"); //YYYY-MM-DD

function formatDate(date) {
  const year = date.getFullYear();
  /* getMonth returns dates from 0, so add one */
  const month = date.getMonth() + 1;
  const day = date.getDate();
  
  return `${year}-${month < 10 ? '0' : ''}${ month }-${ day < 10 ? '0' : '' }${day}`
}

var getDateArray = function(start, end) {

  return formatDate(new Date(
    start.getTime() + Math.random() * (end.getTime(0) - start.getTime(0))
  ));

}
var dateArr = getDateArray(startDate, endDate);


console.log(dateArr);

All I did here was add a call to your formatDate function within the getDateArray function so that now the return of the getDateArray will no longer contain timezone information.

about 4 years ago · Santiago Gelvez Report

0

Create these 2 functions to change the date format.

function DateFormat(startDate)
    {
        const strYear = date("Y",strtotime(strDate));
        const strMonth= date("n",strtotime(strDate));
        const strDay= date("j",strtotime(strDate));
        return "${strYear}$-${strMonth}-${strDay}";
    }

function DateFormat(endDate)
    {
        const strYear = date("Y",strtotime(strDate));
        const strMonth= date("n",strtotime(strDate));
        const strDay= date("j",strtotime(strDate));
        return "${strYear}$-${strMonth}-${strDay}";
    }
about 4 years ago · Santiago Gelvez Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!