Skip to content

Instantly share code, notes, and snippets.

@hassansw
Last active April 25, 2018 09:50
Show Gist options
  • Save hassansw/f140d8ac9c15001c0907a3c0d94fd314 to your computer and use it in GitHub Desktop.
Save hassansw/f140d8ac9c15001c0907a3c0d94fd314 to your computer and use it in GitHub Desktop.
modify and get Date format of your choice :)
//A special thanks to Sabih Siddiqui : https://github.com/siddiquisabih
changeDateToCurrentTimezone(date) {
let splitDate = date.split('-')
let year = parseInt(splitDate[0])
let month = parseInt(splitDate[1])
let splitCurrentDate = splitDate[2]
let newDate = splitCurrentDate.split('T')
let day = parseInt(newDate[0])
let splitTime = newDate[1].split(":")
let hour = parseInt(splitTime[0])
let min = parseInt(splitTime[1])
let sec = parseInt(splitTime[2])
// console.log(hour, min, sec)
// console.log(year, month, day)
var d = new Date()
d.setUTCFullYear(year);
d.setUTCMonth(month - 1);
d.setUTCDate(day);
d.setUTCHours(hour);
d.setUTCMinutes(min);
d.setUTCSeconds(sec);
let showYear = d.getFullYear();
let showMonth = d.getMonth() + 1 < 10 ? "0" + (d.getMonth() + 1) : d.getMonth() + 1;
let showDay = d.getDay() < 10 ? "0" + d.getDay() : d.getDay();
let showHour = d.getHours() < 10 ? "0" + d.getHours() : d.getHours();
let showMinutes = d.getMinutes() < 10 ? "0" + d.getMinutes() : d.getMinutes();
let showSeconds = d.getSeconds() < 10 ? "0" + d.getSeconds() : d.getSeconds();
let returnDate = showYear + "-" + showMonth + "-" + showDay + "T" + showHour + ":" + showMinutes + ":" + showSeconds
return returnDate;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment