0

I'm trying to format some dates using phstc's jquery-dateFormat library (1.0). The problem I'm facing is that for some reason it adds "undefined.undefined" to the formatted Date, as shown in picture when I'm evaluating the variable.

Can someone point me to what is happening here?

Thanks in advance.

var startDate = '@Model.StartDate';
var endDate = '@Model.EndDate';
var format = 'dd.MM.yyyy';
var temp1 = $.format.date(startDate, format);
var temp2 = $.format.date(endDate, format);
$("#startDatePicker").datepicker("setDate", temp1);
$("#endDatePicker").datepicker("setDate", temp2);

Result: "undefined.undefined.04.12.2019"

jquery-dateFormat example

jquery UI attempt

  • Are you bound to this library? Check this out for many examples of formatting dates. https://stackoverflow.com/questions/5250244/jquery-date-formatting – mjw Dec 04 '19 at 20:07
  • Let's say yes, because I may be stupid. Using jquery UI, I'd have to have something like var temp3 = $.datepicker.formatDate(format, new Date(startDate)). Problem is that new Date reads my date as below, instead of my desired format. Fri Apr 12 2019 00:00:00 GMT+0300 (Eastern European Summer Time) With jquery-dateFormat I don't have this issue. – Kitaigorod Alexandru Dec 04 '19 at 21:25
  • also, after I format it with jquery UI, I get "12.April.20192019", which is still weird and I don't get why it's adding 2019 twice. – Kitaigorod Alexandru Dec 04 '19 at 21:26
  • 1
    Try using 2 digit `yy` instead of `yyyy` - are you wanting to format with `.` as separators? – mjw Dec 04 '19 at 21:31
  • https://api.jqueryui.com/datepicker/#utility-formatDate `yy - year (four digit)` – freedomn-m Dec 04 '19 at 21:38
  • yy removes the duplicate 2019. thank you. And yes, I need to use the dot as a separator. Choosing another separator would help it to not see it as MM.dd? – Kitaigorod Alexandru Dec 04 '19 at 21:38
  • Looks like your input (from the images) format doesn't match any of the "expected" input formats. https://github.com/phstc/jquery-dateFormat#expected-input-dates-formats – freedomn-m Dec 04 '19 at 21:41
  • Looks like it's not a general purpose date formater, but needs specific input formats. As you're using MVC/Razor, why not format the date server-side? One method example: `var startDate = '@string.Format("{0:dd.MM.yyyy}", Model.StartDate)';` – freedomn-m Dec 04 '19 at 21:45
  • Server-side I'm using DateTime in my model, and since I need to stick to a format, I wanted to format the date in my View. Your suggestion outputs a string and if I want to "setDate" to my datetimepicker I need to pass Date() as a variable. And I'm back to square one, since new Date() reads it mm/dd/yyyy. – Kitaigorod Alexandru Dec 04 '19 at 21:58
  • And I could change the format when returning the Index View, but I said it's uglier than formatting in my View. – Kitaigorod Alexandru Dec 04 '19 at 22:01

1 Answers1

0

Problem identified. Another colleague was splitting by separator and changing the order of the date, and I wasn't aware of that, hence the "undefined.undefined". So, even if it's said that jquery-dateFormat is accepting only specific formats, it now works perfectly fine with the format from our backend (dd.MM.yyyy HH:mm:ss). Thank you all for your help.