I have a date string in the format dd/mm/yyyy and want to change it.
I want to convert it into format DayOfweek, dd-mm-yyyy. For example 10/7/2016 should be converted to Sun, 10-7-2016.
How can i do this work?
I have a date string in the format dd/mm/yyyy and want to change it.
I want to convert it into format DayOfweek, dd-mm-yyyy. For example 10/7/2016 should be converted to Sun, 10-7-2016.
How can i do this work?
You need to use Date() object to convert string to date and get day name.
var days = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
var dateStr = "10/7/2016";
var date = new Date(dateStr.split("/").reverse().join("-"));
var dayName = days[date.getDay()];
var newFormat = dayName + ", " + dateStr.replace(/\//g, "-");
console.log(newFormat);
You can do this with a function like this:
function date_format(d_str){
var weekdays = [ "Sun","Mon","Tue","Wed","Thu","Fri","Sat"];
var d_s = d_str.split("/");
return weekdays[new Date(d_s[2]+'-'+d_s[1]+'-'+d_s[0]).getDay()]+', '+d_s[0]+'-'+d_s[1]+'-'+d_s[2];
}
console.debug(date_format('10/7/2016'));
Fiddle: https://jsfiddle.net/220tugyx/2/