0

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?

1
  • 1
    Please add the code you've tried so far to the question Commented Jun 15, 2016 at 9:32

2 Answers 2

2

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);

Sign up to request clarification or add additional context in comments.

Comments

1

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/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.