bootstrap-dateppicker的日期格式在页面加载时无效

时间:2015-04-24 13:28:04

标签: asp.net-mvc twitter-bootstrap bootstrap-datepicker

我的datepicker文本框是在ASP.NET MVC中生成的:

$().ready(function() {
    $('.datepicker').datepicker();
});
然后调用

' .datepicker()`:

4/24/2015 12:00:00 AM

似乎有格式,只是不在开头。

首次加载页面时,文本框会显示{{1}}。但是,当选择日期时,格式将适用并显示没有时间戳的日期。

我错过了任何选项或设置吗?

1 个答案:

答案 0 :(得分:0)

如果您希望在客户端上使用您为日期选择器选择的本机格式处理这一切的简单性,您可以通过在页面加载时将日期设置为自身来触发格式化:

struct connection {
    int a, b;
};

void expand(int x, connection *&arr, std::set<int> &exp, int size) {

    for (int i = 0; i < size; i++) {
        if (x == arr[i].a) {
            exp.insert(arr[i].b);
        }
        else if (x == arr[i].b) {
            exp.insert(arr[i].a);
        }
    }

    return;
}

// recursive breadth-first-seach

bool BFSr(std::set<int> &group, std::set<int> &visited, int goal, connection *&arr, int size) {
    if (group.empty()) return false;
    if (group.find(goal) != group.end()) return true;
    std::set<int> tempa;

    for (std::set<int>::iterator it = group.begin(); it != group.end(); ++it) {
        expand(*it, arr, tempa size);
    }

    for (std::set<int>::iterator it = visited.begin(); it != visited.end(); ++it) {
        tempa.erase(*it);
    }

    tempb = visited;
    tempb.insert(group.begin(), group.end());
    return BFSr(tempa, tempb, goal, arr, size);
}

bool BFS(int start, int goal, connection *&arr, int size) {
    std::set<int> tempa;
    std::set<int> tempb;
    tempa.insert(start);
    return BFSr(tempa, tempb, goal, arr, size);
}

int main()
{

    connection *arr = new connection[m];
    connection *con = new connection[m - 1];

    // fill arr with connections arr.a < arr.b ....

    for (int j = 0; j < (m - 1); j++) {
        con[j] = arr[j + 1];
    }

    // 1. edge for performance reasons extra
    if (!BFS(arr[0].a, arr[0].b, con, (m - 1))) {
        // connection is important therefore add to list
        printf(" %d", 1);
    }

    // Look if nodes still connected after removing connection
    for (int s = 1; s < m; s++) {

        con[s - 1] = arr[s - 1];

        if (!BFS(arr[s].a, arr[s].b, con, (m-1))) {
            // connection is important therefore add to list
            printf(" %d", s + 1);
        }
    }

    printf("\n");


    free(arr);
    free(con);

    return 0;
}

或者,这是一个可以添加的扩展方法,它会在调用时重新格式化控件中的日期:

// update and date to trigger formatting
$('#myDate').datepicker('update',$('#myDate').datepicker("getDate"))

然后像这样使用它:

$.fn.reformatDate = function(settings) {  
  return this.each(function() {  
    $this = $(this);
    if (!$this.data('datepicker')) return 
    $this.datepicker('setDate',$this.datepicker("getDate"))
  });
};

Stack Snippets中的演示

&#13;
&#13;
$('#myDate').reformatDate();
&#13;
$.fn.reformatDate = function(settings) {  
  return this.each(function() {  
    $this = $(this);
    if (!$this.data('datepicker')) return 
    $this.datepicker('setDate',$this.datepicker("getDate"))
  });
};

$(function() {
    // simulate pre-filled date from server
    $("#myDate").val(new Date());
  
    // initialize like normal
    $('#myDate').datepicker();
    
    // update date to trigger formatting
    $('#myDate').reformatDate();
});
&#13;
&#13;
&#13;