如何创建完整的日期 [英] how to create complete date
问题描述
如何使用yyyy-MM-dd格式创建日期
i有
how to create date with yyyy-MM-dd format
i have
string month =cbMonth.SelectedItem.Value;
var year = cbYear.SelectedItem.Value;
var date = DateTime.Now.Day;
string FullDate = month+ "-" + year + "-" + date;
cam如何创建完整日期
plz帮助我
how cam create complete date
plz help me
推荐答案
有多种方法可以做到这一点 - 但是使用今天的日期作为日期部分会导致问题。想一想:如果它是2015年1月31日,并且用户输入任何一年的2月,那么您的日期字符串将始终无效。你需要仔细考虑你想要做什么。
忽略这一点(并使用月中的第一个,所以它始终有效)然后这样做:
There are a variety of ways to do this - but using today's date as the day part is going to cause problems. Think about it: if it's 31st Jan 2015, and the user enters "Feb" of any year, your date string is always going to be invalid. You need to think about exactly what you want to do here.
Ignoring that (and using the 1st of the month, so it always works) then this will do it:
string fullDate = string.Format("{0}-{1}-{2}", year, month, 1);
但是如果要创建ISO日期(yyyy-MM-dd)以便将它们发送到SQL,则不要。创建一个DateTime值并通过参数化查询发送它 - 不要连接字符串来构建SQL命令。它让您对意外或故意的SQL注入攻击持开放态度,这可能会破坏您的整个数据库。总是使用参数化查询
如果您的ComboBoxex包含整数值,那么很容易:
But if you are creating ISO dates (yyyy-MM-dd) in order to send them to SQL, then don't. Create a DateTime value and send that instead via a parameterised query - do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Always use Parametrized queries instead
If your ComboBoxex contain integer Values then it's easy:
DataTime fullDate = new DateTime(year, month, 1);
这篇关于如何创建完整的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!