0

I'm getting date like this 09/29/2017 and I want to replace / in - using this in angularjs

$scope.newString = $scope.date.replace("/","-");

But it generate a date like this 09-29/2017 and doesn't replace whole / to - . How can I replace all / to -

Can anyone help me to get this. Any help will be appreciated.

Thanks in advance.

1

3 Answers 3

3

Please try this below code:

$scope.newString = $scope.date.replace(/\//g,"-");

DEMO

var date = "09/29/2017"

var newString = date.replace(/\//g,"-");
alert(newString);

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

3 Comments

it's return me date like 09/29/2017 not like 09-29-2017
@user6891871 oops! I have update my answer. kindly get it now
@user6891871 Please mark as this answer if it helps to you.
1

You can use angular js filter to convert date object according to required format

$scope.newString = $filter('date')($scope.date, "MM-dd-yyyy"); 

More about filter please see this: http://docs.angularjs.org/api/ng.filter:date

2 Comments

I know this a suitable answer but when i add this to my code it looks like nothing to display can you explain a little bit about this filter.
have you inject the filter dependency in your controller ?
0

You can split it based on '/' and join it back using '-'

var date = "09/29/2017"

var newString = date.split("/").join("-");
console.log(newString);

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.