0

I am working with angularjs and the angularjs bootstrap ui (http://angular-ui.github.io/bootstrap/).

I want to use the datepicker directive (http://angular-ui.github.io/bootstrap/#/datepicker).

I am getting a string value from a database

2015-07-30 15:10

Angular wants to have a date object as model but I wasnt able to create a date object with that string. the error I get is

Error: [ngModel:datefmt] http://errors.angularjs.org/1.4.3/ngModel/datefmt?p0=2015-07-30

Can anyboy help me create a date object in angular with a string which has this format -> YYYY-MM-DD

more Information: the string is in my scope

$scope.event.startdate

and I am splitting it in date, hours and minutes.

$scope.startpoint = {
            date: $scope.event.startdate.substring(0, 10),
            hours: $scope.event.startdate.substring(11, 13),
            mins: $scope.event.startdate.substring(14, 16)
        };

The database is MongoDB

Thank you Adrian

eder
  • 51
  • 1
  • 10
  • when you get it from the database are you getting it into the modle with `$scope` or just storing it in a var? also how are you getting it from the database? Is the database SQL or BSON? – Joe Lloyd Aug 11 '15 at 09:31
  • 1
    I have updated my post with the requested information. cheers – eder Aug 11 '15 at 09:36
  • You can consider using momentjs instead of splitting it manually – Mark Aug 11 '15 at 09:38
  • you don't need to split it at all just take the value as it stands and use the date formatter built into angular. Maybe I'm missing something but that should work. – Joe Lloyd Aug 11 '15 at 09:39
  • I use event.startdate (string - 2015-07-30 15:10) as ngModel, but get this error -> Error: [ngModel:datefmt] http://errors.angularjs.org/1.4.3/ngModel/datefmt?p0=2015-07-30. So I need to defined this somehow to an date object which I dont know how to do – eder Aug 11 '15 at 10:50
  • what angular function are you using that requires a date format, I would have thought that `var` would do everything. – Joe Lloyd Aug 11 '15 at 11:46

3 Answers3

3

You can use use a generic javascript solution. For example, you can use serega386's answer to a similar javascript question:

var st = "26.04.2013";
var pattern = /(\d{2})\.(\d{2})\.(\d{4})/;
var dt = new Date(st.replace(pattern,'$3-$2-$1'));
Community
  • 1
  • 1
mani R
  • 361
  • 4
  • 16
  • 2
    Please do not plagiarize. Read this on [How to Properly Reference other answers](http://stackoverflow.com/help/referencing). You answer is a direct copy of [this one](http://stackoverflow.com/a/16235315/4320665). You need to give attribution to the other author and add some original content. – ryanyuyu Aug 11 '15 at 17:00
  • I've edited your answer to give better attribution to the original writer of this code. Please include more explanation for this code to make your answer actually distinct. – ryanyuyu Aug 12 '15 at 00:37
1

You can have the below code in your controller

$scope.newDate =new Date($scope.event.startdate);

and below code in your template

{{newDate | date:'yyyy-MM-dd'}}

Ref: AngularJS/javascript converting a date String to date object

Community
  • 1
  • 1
Sabarish
  • 531
  • 5
  • 17
0

If you are getting the date as a single set of numbers like this

1288323623006

you can use the date formatter directly on that.(from angular doc here)

<span ng-non-bindable>{{1288323623006 | date:'medium'}}</span>:
<span>{{1288323623006 | date:'medium'}}</span><br>
<span ng-non-bindable>{{1288323623006 | date:'yyyy-MM-dd HH:mm:ss Z'}}</span>:
<span>{{1288323623006 | date:'yyyy-MM-dd HH:mm:ss Z'}}</span><br>

Expected result

angular date format

I hope that solves your problem, let me know if I'm missing something.

Joe Lloyd
  • 19,471
  • 7
  • 53
  • 81