0

I have problem with Date format in editing mode. Date is stored as dd/MM/yyyy HH:mm:ss. The date I want to bind in bootstrapdatetime picker should have format MM/dd/yyyy, but viewmodel has the format of DateTime as dd/MM/yyyy and sending same to the edit view.
I tried to change the format by changing CurrentCulture, CurrentUICulture and Parsing etc. But no success.
CurrentCulture of Web-App is set as "pt-BR" but I preffer always "en-US" culture for datepicker, even application culture is set to "pt-BR" or to any other culture.

Model:

[Column(TypeName = "datetime2")]
public DateTime? StartsOn { get; set; }

ViewModel:

public DateTime? StartsOn { get; set; }

Controller:

CultureInfo provider = CultureInfo.CreateSpecificCulture("en-US");
string dateString1="";  
string lang="pt-BR";

dateString1 = entityVM.StartsOn.ToString();
var StartsOn = (DateTime.ParseExact(dateString1, "dd/MM/yyyy HH:mm:ss", provider)
                    .ToString("MM/dd/yyyy", provider));

var outputCulture = CultureInfo.CreateSpecificCulture("en-US");
var inputCulture = CultureInfo.CreateSpecificCulture(lang);

//Just for date format change to en-US culture, and then reset it to application culture
Thread.CurrentThread.CurrentCulture = outputCulture;
Thread.CurrentThread.CurrentUICulture = outputCulture;

//entityVM.StartsOn = (DateTime.ParseExact(StartsOn, "MM/dd/yyyy", outputCulture)); //ok, Format changed here

//Reset culture to pt-BR (application culture)
Thread.CurrentThread.CurrentCulture = inputCulture; // Problem! Format again changed here.
Thread.CurrentThread.CurrentUICulture = inputCulture;

Here is the view:

<div class='input-group date' data-provide="datepicker">
                        @Html.TextBox("StartsOn", Model.StartsOn, new { @class = "form-control", @id = "datetimepicker1", @type = "text", @required = true })
                        <span class="input-group-addon">
                            <span class="glyphicon glyphicon-calendar"></span>
                        </span>
</div> 

<script type="text/javascript">
            $(document).ready(function () {
                $('.datepicker').datepicker();

            });
</script>

The problem is that I always get dd/MM/yyyy instead of MM/dd/yyyy. How can it be converted to MM/dd/yyyy ?

3 Answers 3

0

You need to specify date format for the datepicker:

$('.datepicker').datepicker({
        dateFormat: 'mm/dd/yy'
    });
0
0

Is the datepicker showing properly? You are using the datepicker using .datepicker but I can't see any class mentioned as datepicker. Did you mean its showing different format populating it from model?

4
  • Yes, datepicker is working fine as per their documentation. Currently its showing what Viewmodel is having. I want to change format of Datetime in Viewmodel.
    – kpr
    Feb 16, 2016 at 12:46
  • Ok, so the issue is not with datepicker, razor by default binds the default .ToString() format of the data type to the text-box. One easy solution is to use string in view-model and DateTime in domain-model. Will that be of any help?
    – SamGhatak
    Feb 16, 2016 at 12:54
  • I also put class as datepicker. But problem is same. I use Auto-mapper and it will be added work if I go with your suggestion.
    – kpr
    Feb 16, 2016 at 13:26
  • What I mean is: Model to ViewModel: string startOn = m.StartOn.ToString("dd/MM/yyyy HH:mm:ss") and ViewModel to Model: bool success = DateTime.TryParseExact(model.StartOn, myformat, CultureInfo.InvariantCulture, DateTimeStyles.None, out m.StartOn)
    – SamGhatak
    Feb 16, 2016 at 14:04
0

Your viewmodel is correct and NOT dd/MM/yyyy. dd/MM/yyyy, MM/dd/yyyy ... are all "display" formats only. Underlying value is DateTime? and is independent of the display format. With jquery ui datepicker the default display format (dateFormat) is already "mm/dd/yy". With bootstrap datepicker it is format: "mm/dd/yyyy" (and it is again the default).

I think your problem is that, you are converting a datetime value to a string and then parsing back to a datetime. I have no idea why you are doing that. If you would do that why don't you also make to ToString("dd/MM/yyyy HH:mm:ss")?

dateString1 = entityVM.StartsOn.ToString("dd/MM/yyyy HH:mm:ss");
var StartsOn = (DateTime.ParseExact(dateString1, "dd/MM/yyyy HH:mm:ss", provider)
        .ToString("MM/dd/yyyy", provider));

IMHO this is a totally unnecessary conversion. It should simply be:

var StartsOn = entityVM.StartsOn;
//or:
var StartsOn = (DateTime)entityVM.StartsOn;
3
  • But your suggestion does not give MM/dd/yyyy format. My code gives this, though it is a string. Then I parse it to datetime, it also works but when I reset the culture It again converts automatically to dd/MM/yyyy due to pt-BR default culture.
    – kpr
    Feb 16, 2016 at 13:22
  • I don't understand what you mean here. You set the display format using the "format" on the bootstrap datepicker. Its default is mm/dd/yyyy, you can make it MM/dd/yyyy if you wish. I am telling you that on conroller side, it comes in as a DateTime?, you shouldn't play with it converting to a string and then back to a datetime. Feb 16, 2016 at 14:27
  • ok, I removed the all extra code for date format in controller and also 'culture-change' code there in. So it is in dd/mm/yyyy HH:mm:ss format now. In view I set manually default datepicker format as mm/dd/yyyy. But still it is displaying as dd/mm/yyyy 00:00:00 format. When I click the date in text field of datepicker then it shows the correct format. Date is also not same. It is the date on which I clicked on datepicker.
    – kpr
    Feb 17, 2016 at 6:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.