18 January 2026:


This forum is now archived and is in read-only mode. Please continue discussions on our improved new Sahi Pro Community forum.



Sahi Pro is an enterprise grade test automation platform which can automate web, mobile, API, windows and java based applications and SAP.

How to generate a random date

vijaysaimsvijaysaims Members
edited November -1 in Sahi - Open Source
How can I generate a random date using javascript. The date should be in DD/MM/YYYY format

Comments

  • mohanmohan Members
    edited March 2012
    Hi ..

    I am using this js functions for date and time

    function currentYear(){
    var today = new Date();
    var dd = today.getDate();
    var mm = today.getMonth();
    var yyyy = today.getFullYear();
    if(dd<10){dd='0'+dd}
    if(mm<10){dd='0'+mm}
    {
    return mm+dd+yyyy;
    }
    }
  • mohanmohan Members

    function currentTime(){
    var currentTime = new Date();
    var hours = currentTime.getHours();
    var minutes = currentTime.getMinutes();
    var seconds = currentTime.getSeconds();
    if (minutes < 10){
    minutes = "0" + minutes;
    }

    if (seconds < 10){
    seconds = "0" + seconds;
    }


    if(hours > 11){
    return hours + "" + minutes + "" + seconds;
    }
    else {
    return hours + "" + minutes + "" + seconds;
    }
    }
  • This script generates a random past date in dd/mm/yyyy format

    function currentYear()
    {
    var $today = new Date();
    var $dd = $today.getDate();
    var $mm = $today.getMonth();
    var $yyyy = $today.getFullYear();
    $rand = Math.floor(Math.random()*11);
    $dd = $rand+$dd;
    $mm = $rand+$mm;
    $yyyy = $yyyy-$rand;
    if($dd>28) { $dd = 28; }
    if($mm>12) { $dd = 12; }
    if($dd<10){$dd='0'+$dd}
    if($mm<10){$mm='0'+$mm}
    $randDate=$dd+"/"+$mm+"/"+$yyyy;
    {
    return $randDate;
    }
    }
    currentYear();
    _alert($randDate);

    Thank You mohan!!!
Sign In or Register to comment.