1

My model has property TimeScheduleOnUtc like this

[DisplayFormat(DataFormatString = "{0:MM/dd/yyyy HH:mm}")]
public DateTime? TimeScheduleOnUtc { get; set; }

My form has

<input type="text" class="form-control datetimepicker" placeholder="MM/dd/yyyy HH:mm" name="TimeScheduleOnUtc" />

But when I submit the form I always get a null value in TimeScheduleOnUtc. It just happened on my computer and all of my members seem they work fine. I can't find a reason why, and how can I fix. I think it may be a bug depends on date time configuration on my computer.

Ali Soltani
  • 9,589
  • 5
  • 30
  • 55
Dũng Kon
  • 69
  • 2
  • 11

2 Answers2

1

Check whether your application culture supports this date format. I had some problems with the exactly same date format. And the solutions was to set change the current culture in the constructor of my controller.

Try this:

Thread.CurrentThread.CurrentCulture = new CultureInfo("en-GB");

Additional: to check all of the supported date-time formats you can use the code from my gist code directly in your view

@{ 
    var dateInfo =  System.Threading.Thread.CurrentThread.CurrentCulture.DateTimeFormat;
}
    @foreach (var item in dateInfo.GetAllDateTimePatterns())
    {

        <div>@item</div>
    }

I believe there is a better approach to solve the problem, but this could be used as a workaround for now.

mihkov
  • 1,171
  • 13
  • 37
0

Thank you for all of your answers. Finally, I found the solution is that I need to change my datetimepicker config in my javascript from

        $('.datetimepicker')
            .datetimepicker({
                format: 'mm/dd/yyyy hh:ii',
                autoclose: true,
                startDate: new Date()
            });

To

      $('.datetimepicker')
        .datetimepicker({
            format: 'MM/dd/yyyy hh:ii',
            autoclose: true,
            startDate: new Date()
        });

So that I change mm to MM. It works fine on my computer but maybe on your computers, it doesn't. So be careful to use it. I hope it maybe helpful for you in the future.

Dũng Kon
  • 69
  • 2
  • 11
  • Is that a correct format for the minutes `ii` ? I've never seen that in `JavaScript` or `ASP .NET MVC ` – mihkov Oct 18 '17 at 11:46