Textview的文本中包含日期(;
),其格式为DateTime.Now.LocalTime
。我尝试通过7/4/2016 3:32:40 PM
传递这个但是给了我SqlDateTime溢出错误,所以我尝试将其更改为DateTime time = Convert.ToDateTime(time.Text);
但是给了我一个不正确的格式错误。可能是什么问题?
以下是我的代码:
DateTime time = DateTime.ParseExact(txt.Text, "MM/dd/yyyy hh:mm:ss tt", CultureInfo.InvariantCulture);
相关代码:
TextView txt = FindViewById<TextView>(Resource.Id.txt);
DateTime time = DateTime.ParseExact(txt.Text, "MM/dd/yyyy hh:mm:ss tt", CultureInfo.InvariantCulture);
// to pass to webservice
userInfo.TimeStamp = time; // TimeStamp is DateTime type
我在网络服务中的代码:
private void DialogBox(object sender, EventArgs eventArgs) // this is for inserting the DateTime value into database
{
EmployeeDetails userInfo = new EmployeeDetails();
DateTime now = DateTime.Now.ToLocalTime();
TextView txt = FindViewById<TextView>(Resource.Id.txt);
if (String.IsNullOrWhiteSpace(request.Text))
{
Android.Support.V7.App.AlertDialog.Builder builder = new Android.Support.V7.App.AlertDialog.Builder(this, Resource.Style.AppCompatAlertDialogStyle);
builder.SetTitle("Warning!");
builder.SetIcon(Resource.Drawable.warning);
builder.SetMessage("Please specify your comment or suggestion.");
builder.SetPositiveButton("OK", (s, ev) => { });
builder.Show();
}
else
{
txt.Text = now.ToString(); // this is for getting the value of passed datetime for next use
userInfo.TimeStamp = now;
_client.InsertToFeedbackTableAsync(userInfo);
}
}
private void ClientOnInsertToFeedbackTableCompleted(object sender, InsertToFeedbackTableCompletedEventArgs ea)
{
EmployeeDetails userInfo = new EmployeeDetails();
TextView txt = FindViewById<TextView>(Resource.Id.txt);
DateTime time = DateTime.ParseExact(txt.Text, "MM/dd/yyyy hh:mm:ss tt", CultureInfo.InvariantCulture);
string msg = null;
if (ea.Error != null)
{
//blahblah
}
else if (ea.Cancelled)
{
//blahblah
}
else
{
msg = null;
RunOnUiThread(() =>
{
userInfo.TimeStamp = time;
_client.InsertToAnswersTableRequestAsync(userInfo);
});
}
}
private void ClientOnInsertToAnswersTableRequestCompleted(object sender, InsertToAnswersTableRequestCompletedEventArgs ea)
{
EmployeeDetails userInfo = new EmployeeDetails();
TextView txt = FindViewById<TextView>(Resource.Id.txt);
DateTime time = DateTime.ParseExact(txt.Text, "MM/dd/yyyy hh:mm:ss tt", CultureInfo.InvariantCulture);
string msg = null;
if (ea.Error != null)
{
//blahblah
}
else if (ea.Cancelled)
{
//blahblah
}
else
{ // blahblah}
答案 0 :(得分:0)
由于您的日期时间值(例如7/4/2016 3:32:40 PM
)可以包含单个数字,不一定包含2位数字,因此您应该使用格式字符串"M/d/yyyy h:m:s tt"
到DateTime.ParseExact
。