UZAIR UL HAQ
UZAIR UL HAQ

Reputation: 3

How can i get day from google sheet via google script

I want to get a date google cell and return the zodiac sign. I have a working algorithm. But I am unable to get day and month from the date(am getting from google sheet cell)

Fucntion GetDate(DB) // Getting date from Google sheet
{
var date = Utilities.formatDate(new Date(DB), "GMT+5", "MM/dd/yyyy"); // Date Format

var day = // How can i pass day
var month = // How can i pass month

function zodiac_sign(day,month) 
{ 
    Var astro_sign=""; 


    if (month == "december"){ 

        if (day < 22) 
        astro_sign = "Sagittarius"; 
        else
        astro_sign ="capricorn"; 
    } 

    else if (month == "january"){ 
        if (day < 20) 
        astro_sign = "Capricorn"; 
        else
        astro_sign = "aquarius"; 
    } 

    else if (month == "february"){ 
        if (day < 19) 
        astro_sign = "Aquarius"; 
        else
        astro_sign = "pisces"; 
    } 

    else if(month == "march"){ 
        if (day < 21)  
        astro_sign = "Pisces"; 
        else
        astro_sign = "aries"; 
    } 
    else if (month == "april"){ 
        if (day < 20) 
        astro_sign = "Aries"; 
        else
        astro_sign = "taurus"; 
    } 

    else if (month == "may"){ 
        if (day < 21) 
        astro_sign = "Taurus"; 
        else
        astro_sign = "gemini"; 
    } 

    else if( month == "june"){ 
        if (day < 21) 
        astro_sign = "Gemini"; 
        else
        astro_sign = "cancer"; 
    } 

    else if (month == "july"){ 
        if (day < 23) 
        astro_sign = "Cancer"; 
        else
        astro_sign = "leo"; 
    } 

    else if( month == "august"){ 
        if (day < 23)  
        astro_sign = "Leo"; 
        else
        astro_sign = "virgo"; 
    } 

    else if (month == "september"){ 
        if (day < 23) 
        astro_sign = "Virgo"; 
        else
        astro_sign = "libra"; 
    } 

    else if (month == "october"){ 
        if (day < 23) 
        astro_sign = "Libra"; 
        else
        astro_sign = "scorpio"; 
    } 

    else if (month == "november"){ 
        if (day < 22) 
        astro_sign = "scorpio"; 
        else
        astro_sign = "sagittarius"; 
    } 

    return astro_sign; 
}

}

Upvotes: 0

Views: 3304

Answers (1)

Cooper
Cooper

Reputation: 64032

First of all function is spelled wrong.

Fucntion GetDate(DB) // Getting date from Google sheet
{
var date = Utilities.formatDate(new Date(DB), "GMT+5", "MM/dd/yyyy"); // Date Format

var day = // How can i pass day
var month = // How can i pass month

How about this instead:

function GetDate(DB) {
  var dt=new Date(DB);
  var date=Utilities.formatDate(dt, "GMT+5", "MM/dd/yyyy");
  var day = Utilities.formatDate(dt, "GMT+5", "dd");//or possibly dt.getDate();
  var month = Utilities.formatDate(dt, "GMT+5", "MM");//or possibly dt.getMonth()+1;

In the code above the Utilities.formatDate() returns a string. But dt.getDate() returns a number from 1 to 31 and dt.getMonth() returns a number from 0 to 11. Generally, people add one to the month but it depends upon what you're doing with it.

JavaScript Date Object

Upvotes: 1

Related Questions