[英]using database dates in datepicker in asp.net c#
I have a datepicker which needs some dates to block. 我有一个日期选择器,需要一些日期才能阻止。 I need to fetch the date from database.
我需要从数据库中获取日期。 The below script is working fine.
下面的脚本工作正常。
var disabledDates = ["2017-11-28", "2017-11-14", "2017-11-21"];
$(function () {
$('.dtpkr').datepicker(
{dateFormat: 'dd/mm/yy',
appendText: '(dd/mm/yy)',
showOn: 'both',
beforeShowDay: function (date) {
var string = jQuery.datepicker.formatDate('yy-mm-dd', date)
return [disabledDates.indexOf(string) == -1]
}
});
})
I am using following code to bring dates from database. 我正在使用以下代码从数据库中获取日期。
string date_query = "select display_date from X where display_type='static'";
DataTable dt = objDUT.GetDataTable(date_query);
ArrayList arrayList = new ArrayList();
for (int i = 0; i <= dt.Rows.Count - 1; i++)
{
arrayList.Add(dt.Rows[i][0].ToString());
}
Newtonsoft.Json.JsonSerializer jsr = new Newtonsoft.Json.JsonSerializer();
System.IO.StringWriter sw = new System.IO.StringWriter();
Newtonsoft.Json.JsonTextWriter jtw = new Newtonsoft.Json.JsonTextWriter(sw);
jsr.Serialize(jtw, arrayList);
string jsArrayJSON = sw.ToString();
I am getting the dates like 我正在约会
12/12/2012 12:00:00 AM
12/12/2012 12:00:00 AM
but I want to have the dates like ["2017-11-28", "2017-11-14", "2017-11-21"]
但我想使用类似
["2017-11-28", "2017-11-14", "2017-11-21"]
how to achieve that? 如何实现的?
Your for
loop is the issue. 您的
for
循环是问题。 It's explicitly converting what's probably a date or datetime field in your DataTable
to a string. 它明确地将有什么可能在你一个日期或日期时间字段
DataTable
为一个字符串。
Instead, explicitly convert it to a DateTime
, and then use string.Format()
or string interpolation to convert it to a string in the proper format. 而是将其显式转换为
DateTime
,然后使用string.Format()
或字符串插值将其转换为正确格式的字符串。
string.Format(): string.Format():
for (int i = 0; i <= dt.Rows.Count - 1; i++)
{
arrayList.Add(string.Format("{0:MM/dd/yyyy}", Convert.DateTime(dt.Rows[i][0]));
}
String interpolation: 字符串插值:
for (int i = 0; i <= dt.Rows.Count - 1; i++)
{
arrayList.Add("{Convert.DateTime(dt.Rows[i][0]):MM/dd/yyyy}");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.