How to bind a string variable to datepicker control instead of a date object?

Osama Javed

I am using Angular UI datepicker in my project.

The control has an option "datepicker-popup" which allows me to set up te format I want to display the date in. However the date is bound to my model as a date object and not as a formatted string.

The rest of my code simply requires the date as a string in the correct (yyyy-MM-dd) format.

At the moment wehenever I need to use the date, I format it to the correct string before passing it along.

This works for now since The code base is pretty small but is there a better way to somehow bind the date to my model as a string so that someone forgetting to format the date before using it doesn't break the system.

A plunker for the sample code can be found here.

I was thinking maybe I would need to set up a watch or something but was not too sure what the correct solution would be.

SoluableNonagon

No, currently AngularUI and many other frameworks use the Date object to bind information. You need to format the date to a string each time you want it as a string. The way to do this is to create a function like

$scope.getMyDateAsString = function(){
    return myDate.toString(); // or however you format your string.
};

Then anytime you want to get the string you can call this function. You CAN create a watcher

$scope.$watch($scope.myDateModel, function(newVal, oldVal){
    $scope.myDateAsString = $scope.getMyDateAsString();
});

This way, anytime the datepicker changes value, you change the value of the string.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to bind control to object property?

From Dev

How to bind control to object property?

From Dev

How to parse a json object property to bind it to a control

From Dev

How to bind a string to object in java

From Dev

Why does initializing a date object fail when I use a variable instead of integers or a string?

From Dev

How to get Selected Text from WebBrowser WPF Control and bind it to a string object?

From Dev

How to Bind Data to kendo grid by passing date from kendo datepicker?

From Dev

How to bind enabled/disabled status to a control instead of view model?

From Dev

How to make Bootstrap Datepicker returns number of days instead of date?

From Dev

How to store value from datepicker into string variable

From Dev

How to store value from datepicker into string variable

From Dev

How to bind a xml element into an object member variable?

From Dev

How to bind a JavaFX 8 object to a java variable?

From Dev

Bind string to control

From Dev

jQuery: pass string variable to date object

From Dev

How to parse String to Date object?

From Dev

How do you set the date value on Angular Material datepicker with a variable?

From Dev

How to identifiy date string vs date object

From Dev

How to compare a string variable with a date variable

From Dev

Bind Control Resource variable to DataContext

From Dev

Bind an object with array variable

From Dev

Bind an object with array variable

From Dev

getting a string instead of a DateTime object (Symfony 2.8 and date form fields)

From Dev

How to return object instead of string for response with nock?

From Dev

AngularJS how return a string instead of Json object

From Dev

Bootstrap datepicker -- how to get date as a string in the right format?

From Dev

How to format ng-model string to date for angular material datepicker

From Dev

Parse date string to Date object when loading Angular UI Bootstrap Datepicker

From Dev

Datepicker date formatting return minute instead of month

Related Related

  1. 1

    How to bind control to object property?

  2. 2

    How to bind control to object property?

  3. 3

    How to parse a json object property to bind it to a control

  4. 4

    How to bind a string to object in java

  5. 5

    Why does initializing a date object fail when I use a variable instead of integers or a string?

  6. 6

    How to get Selected Text from WebBrowser WPF Control and bind it to a string object?

  7. 7

    How to Bind Data to kendo grid by passing date from kendo datepicker?

  8. 8

    How to bind enabled/disabled status to a control instead of view model?

  9. 9

    How to make Bootstrap Datepicker returns number of days instead of date?

  10. 10

    How to store value from datepicker into string variable

  11. 11

    How to store value from datepicker into string variable

  12. 12

    How to bind a xml element into an object member variable?

  13. 13

    How to bind a JavaFX 8 object to a java variable?

  14. 14

    Bind string to control

  15. 15

    jQuery: pass string variable to date object

  16. 16

    How to parse String to Date object?

  17. 17

    How do you set the date value on Angular Material datepicker with a variable?

  18. 18

    How to identifiy date string vs date object

  19. 19

    How to compare a string variable with a date variable

  20. 20

    Bind Control Resource variable to DataContext

  21. 21

    Bind an object with array variable

  22. 22

    Bind an object with array variable

  23. 23

    getting a string instead of a DateTime object (Symfony 2.8 and date form fields)

  24. 24

    How to return object instead of string for response with nock?

  25. 25

    AngularJS how return a string instead of Json object

  26. 26

    Bootstrap datepicker -- how to get date as a string in the right format?

  27. 27

    How to format ng-model string to date for angular material datepicker

  28. 28

    Parse date string to Date object when loading Angular UI Bootstrap Datepicker

  29. 29

    Datepicker date formatting return minute instead of month

HotTag

Archive