日期在文本框中输入格式 [英] date enter formate in text box

查看:98
本文介绍了日期在文本框中输入格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用文本文件获取日期并存储在mysql中(mysql日期格式是year-month-day),但我想用户输入(mm/dd/yy)格式并存储在mysql

i am use a text filed to get the date and store in mysql (mysql date formate is year- month-day) but i want to user enter (mm/dd/yy) formate and store in mysql

推荐答案

根据偏好,请勿使用文本框.
并且不要强迫用户使用任何特定格式-让他们使用自己喜欢的格式.他们将把PC设置为该区域设置和格式,因此让他们按原样输入它.

如果您使用 DateTimePicker控件 [ DateTime.ParseExact转换为DateTime [ ^ ]方法.

如果将其作为参数化查询传递给MySQL,则可以将其传递给DateTime值,而不必担心MySQL内部格式.不要将其转换为字符串并进行连接,这是灾难的根源!
By preference, don''t use a textbox.
And don''t force the user to use any particular format - let them use the format that they are comfortable with. They will have set their PC to that locale and format, so let they enter it as they will.

If you use a DateTimePicker control[^] it lets them use what they want, and ensures that all dates are valid.

If you really must fix the format, then you need to look ate doing a conversion to DateTime via the DateTime.ParseExact[^] method.

If you hand it to MySQL as a parameterized query, you can then pass it a DateTime value without having to worry about MySQL internal formats. Don''t convert it to a string and concatenate, it is a recipe for disaster!
MySqlCommand cmd = new MySqlCommand("INSERT INTO myTABLE (date) VALUES (@DT)", conn);
cmd.Parameters.AddWithValue("@DT", myDateTimePicker.Value);


这篇关于日期在文本框中输入格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆