0

Please this "15:00" is coming to me as string.

I want convert it into 3:00 pm; means I want to convert it from GMT to EST.

Please tell me how to do it by inbuilt function or by creating some own function?

mplungjan
  • 169,008
  • 28
  • 173
  • 236
  • 2
    Possible duplicate of [How to use timezone offset in Nodejs?](http://stackoverflow.com/questions/10615828/how-to-use-timezone-offset-in-nodejs) – David R Apr 19 '17 at 12:22

1 Answers1

0

I wrote a function which could fit your needs:

function convertTime(time){
  var arr = time.split(':'); //splits the string
  var hours = Number(arr[0]); 
  var minutes = Number(arr[1]);
  var AMPM = "";
  if(hours>=12 && hours != 24){ //checks if time is after 12 pm and isn't midnight
    hours = hours-12;
    AMPM = " pm";
  }else if(hours<12){//checks if time is before 12 pm
    AMPM = " am";
  }

  if(hours==24){//special case if it is midnight with 24
    hours = hours - 12;
    AMPM = " am"
  }

  if(hours==0){//special case if it is midnight with 0 
    hours = hours + 12;
  }

  //converts the Numbers back to a string
  var sHours = hours.toString();
  var sMinutes = minutes.toString();
  if(hours<10){//checks if the hours is 2 places long and adds a 0 if true
    sHours = "0" + sHours;
  }
  if(minutes<10){//checks if the minutes is 2 places long and adds a 0 if true
    sMinutes = "0" + sMinutes;
  }

  var result = sHours + ":" + sMinutes + AMPM; //sets string back together
        return result;
}

https://jsfiddle.net/b059upu8/

Marco
  • 517
  • 6
  • 19