我知道我可以用这个来计算两个日期之间的天差:
private int DaysFromStart(DateTime date, DateTime startDate)
{
var days = date.Subtract(startDate);
return (int)days.TotalDays;
}
Run Code Online (Sandbox Code Playgroud)
但现在我想通过仅包括工作日获得两个日期之间的差异.因此,例如,如果开始日期是星期五,要检查的日期是下一个星期一,则返回值将为1.
使用C#,我试图将日期格式化为以下字符串格式:YYYYMMDD_HHMM.xlsx
这是我的代码:
DateTime.Today.AddDays(0).ToString("yyyymmdd") + "_" + DateTime.Today.AddDays(0).ToString("hhmm")
Run Code Online (Sandbox Code Playgroud)
这是我的输出:
20130027_1200.xlsx
Run Code Online (Sandbox Code Playgroud)
月份不正确,时间也不正确.
使用下面的DateTime.TryPaseExact方法可以在预期的时候给出不同的输出.
我从以下代码返回以下格式:
2014-02-10 18:32:37
1402-10-18 17:23:00 (which is clearly incorrect)
Run Code Online (Sandbox Code Playgroud)
我如何才能获得yyyy-MM-dd HH:mm:ss返回的格式?
static readonly string[] DateFormats = new string[] { "yyMMddHHmm", "yyyyMMddHHmm", "yyMMddHHmmss", "yyyy-MM-dd HH:mm:ss" };
DateTime date;
DateTime.TryParseExact("140211090915", DateFormats, CultureInfo.InvariantCulture,
DateTimeStyles.AssumeLocal, out date);
Run Code Online (Sandbox Code Playgroud)
此外,如果有人可以解释为什么此140211084947输入返回预期的格式并140211090915返回不需要的格式,我将不胜感激
我有一个布尔告诉我,如果当前时间介于两个其他时间之间,它将在晚上8点(20点)变暗,早上7点点亮.我不确定我是否应该做7或07我试过摊位但仍然变得虚假?
谁能告诉我为什么这总是返回假?没有什么可说的,只是当它目前处于两次之间时总是返回假.. GTM Timezone London,谢谢!
public static bool NightTime
{
get
{
TimeSpan span = LastMoodlightUpdate - DateTime.Now;
TimeSpan start = new TimeSpan(20, 0, 0); //Dark at 20:00
TimeSpan end = new TimeSpan(07, 0, 0); //Light at 07:00
TimeSpan now = DateTime.Now.TimeOfDay;
return ((now > start) && (now < end));
}
}
Run Code Online (Sandbox Code Playgroud) 我想在LINQ选择中将日期和时间转换为波斯语,但linq无法识别我的方法:
LINQ to Entities无法识别方法'System.String toPersianDateTime(System.DateTime)'方法,并且此方法无法转换为存储表达式.
如何将我的方法更改为LINQ兼容?
我的方法:
public static string toPersianDateTime(DateTime dt)
{
PersianCalendar pc = new PersianCalendar();
string pDateTime = pc.GetYear(dt).ToString() + "/" + pc.GetMonth(dt).ToString() + "/" + pc.GetDayOfMonth(dt).ToString() + " ";
pDateTime += pc.GetHour(dt) + ":" + pc.GetMinute(dt) + ":" + pc.GetSecond(dt);
return pDateTime;
}
Run Code Online (Sandbox Code Playgroud)
我的LINQ代码:
var result = (from ord in db.vw_orders
where ord.uid == user.id
orderby ord.order_date descending
select new { ord.id,
date = Tools.toPersianDateTime((DateTime)ord.order_date),
ord.is_final,
ord.status,
ord.image_count,
ord.order_count,
ord.total_price });
Run Code Online (Sandbox Code Playgroud) 我有这 2 个数据表,customerTableDT并且customerAliasesTableDT. 它们都是从数据库中填充的,如下所示:
customerTableDT = UtilityDataAndFunctions.PerformDBReadTransactionDataTableFormat(String.Format("SELECT * FROM {0}", TableNames.customers));
customerAliasesTableDT = UtilityDataAndFunctions.PerformDBReadTransactionDataTableFormat(String.Format("SELECT * FROM {0}", TableNames.customerAliases));
Run Code Online (Sandbox Code Playgroud)
现在我尝试对两个数据表进行内部联接,如下所示:
var customerNames = from customers in customerTableDT.AsEnumerable()
join aliases in customerAliasesTableDT.AsEnumerable on customers.Field<int>("CustomerID") equals aliases.Field<int>("CustomerID")
where aliases.Field<string>("Alias").Contains(iString) select customers.Field<string>("Name")
Run Code Online (Sandbox Code Playgroud)
但它给了我这个错误:
The type of one of the expressions in the join clause is incorrect. Type inference failed in the call to 'Join'.
Run Code Online (Sandbox Code Playgroud)
如果我必须用 SQL 编写我想要做的事情,那么它非常简单:
SELECT * FROM CUSTOMERS C
INNER JOIN CustomerAliases ALIASES ON ALIASES.CustomerID = C.CustomerID …Run Code Online (Sandbox Code Playgroud) 如何在Json下面使用JsonConvert.DeserializeObject
[{
"attributes" : {
"type" : "User",
"url" : "/xx/xx/xx"
},
"Id" : "1",
"Name" : "abc"
},{
"attributes" : {
"type" : "User",
"url" : "/xx/xx/xx"
},
"Id" : "2",
"Name" : "abc"
},{
"attributes" : {
"type" : "User",
"url" : "/xx/xx/xx"
},
"Id" : "3",
"Name" : "abc"
}]
Run Code Online (Sandbox Code Playgroud)
这些是我的班级
public class Attributes
{
public string type { get; set; }
public string url { get; set; }
}
public class RootObject
{
public Attributes attributes …Run Code Online (Sandbox Code Playgroud) 我创建了一个构造函数如下:
public Animal(string regNum, DateTime brought, string name)
{
this.RegNumber = regNum;
this.DateBrought = brought;
this.Name = name;
this.NameNewOwner = null;
}
Run Code Online (Sandbox Code Playgroud)
基于上面的构造函数,我创建了一个名为的对象pet,如下所示:
Animal pet = new Animal("a12344", Convert.ToDateTime(23/01/2013), "Fluffy");
Run Code Online (Sandbox Code Playgroud)
但是,当我运行我的程序时,它会给我一个错误说:Invalid cast from Int32 to DateTime任何人都可以帮我这个吗?
您好,感谢您的阅读.
我正在尝试将当前日期插入到我的表中,但我无法弄清楚如何正确编写它.
这是我的C#代码:
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["LocalSqlServer"].ConnectionString);
conn.Open();
string Comment = UserWriteComment.Text;
string ID = DetailedID.Text;
string Name = DetailedName.Text;
string UniqueID = lblID.Text;
string query = "INSERT INTO Comment(TicketID, Name, Comments, UserID, Date)" + "Values('" + ID + "', '" + Name + "', '" + Comment + "', '" + UniqueID + "', '" + Date + "')";
using (SqlCommand com = new SqlCommand(query, conn))
{
com.ExecuteNonQuery();
UserWriteComment.Text = "";
}
Run Code Online (Sandbox Code Playgroud)
在Query中,有一个名为Date的值.这是我喜欢将当前日期传递到我的表中的函数.
我希望你能帮助我,因为我没有设法找到答案.
谢谢:)
我试图使用IQueryable Include方法加载导航属性,但是虽然表达式是正确的我没有得到任何结果
这是代码
protected void LoadNavigationProperty(ref IQueryable<T> query, Expression<Func<T, object>>[] navigationProperties)
{
if ((query != null) && (navigationProperties != null))
{
foreach (Expression<Func<T, object>> navigationProperty in navigationProperties)
{
query.Include<T, object>(navigationProperty);
}
}
}
Run Code Online (Sandbox Code Playgroud)
我在查询上设置了一个断点.包括并检查数据:
navigationProperties[0] = { n => n.UserStatus }
navigationProperties[1] = { n => n.PrivilegeLevel }
Run Code Online (Sandbox Code Playgroud)
在单步执行包含行之后,我再次检查了查询值,发现它不包含导航属性
c# ×10
datetime ×4
.net ×1
constructor ×1
inner-join ×1
iqueryable ×1
json ×1
json.net ×1
linq ×1
object ×1
parameters ×1
sql ×1
timespan ×1