繁体   English   中英

如何将UtcNow分配给DateTime选择器值属性?

[英]How to assign UtcNow to a DateTime picker value property?

从这里使用Bootstrap 3 DateTime选择器 在加载Razr表单时,我想将默认时间设置为当前UtcNow时间戳。

为了设置此默认值,我尝试了多种不同的DateTime格式。 但是控件似乎不接受传入的格式。

尝试了一些格式:

//Format #1
DateTime.UtcNow.ToString("yyyyMMddHHmmtt");

//Format #2
string.Format("{0:g}", DateTime.UtcNow);

我知道DateTime选择器在选择时间时使用的格式如下,即07/13/2016 2:43 AM ,所以我认为如果我可以将UTC时间转换为这种格式,它将接受时间戳。

我验证了可以使用相同的Razr语法将此值分配给常规输入元素,从而排除了未传递值的问题。

题:

如何将UtcNow分配给DateTime选择器默认值属性?

控制器:在控制器中,我传入string.Format("{0:g}", DateTime.UtcNow); 作为模型属性。

//Create a dynamic object to store list of different model types.
dynamic dynamicEscalationModel = new ExpandoObject();
dynamicEscalationModel.OutageStartTime = string.Format("{0:g}", DateTime.UtcNow);

查看:然后我使用@符号将此值分配给DateTime选择器:

<!-- Outage Start -->
<div class="form-group">
    <label class="col-md-9 control-label" style="text-align: left;" for="OutageStartDisplay">Outage Start (*UTC)</label>
    <div class="col-md-9">
        <div class='input-group date' id='OutageStartDisplay'>
            <input type='text' class="form-control" id="OutageStartDisplayInput" name="OutageStartDisplayInput" value="@Model.OutageStartTime" />
            <span class="input-group-addon" id="OutageStartDisplayCalendar">
                <span class="glyphicon glyphicon-calendar"></span>
            </span>
        </div>
    </div>
</div>

为了进行测试,我添加了第二个普通输入,并且正确绑定了model属性:

<input type='text' class="form-control" id="" name="" value="@Model.OutageStartTime" />

我的脚本标签中的DateTimepicker初始化:

//Init the Outage Start date time picker
$(function () {
    $('#OutageStartDisplay').datetimepicker({
    });
});

由于您现在正在获取UTC,所以为什么不直接从客户端进行操作:

    <script type="text/javascript">
        $(function () {
            var t = new Date(); 
            t.setMinutes(t.getMinutes() + t.getTimezoneOffset());

            $('#OutageStartDisplay').datetimepicker({
                defaultDate: t,
            });
        });
    </script>

将此添加到脚本标签中,您需要在此处初始化它:

      $('#OutageStartDisplay').datetimepicker({
        format: 'm/d/Y H:i:s',
        theme: 'dark',
        startDate: '@Model.OutageStartTime'
    });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM