473,385 Members | 1,542 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Problem in setting DateTime value explicitly.

88 64KB
I want to pick a value from a DateTimePicker in 'MM/dd/yyyy hh:mm tt' format, add 00 as second with it, store it in a DateTime variable and save it to database. Suppose if I pick the value 05/28/2010 9:25 AM or 05/28/2010 9:25:34 AM from the DateTimePicker, it'll be stored in the variable and saved to database as 05/28/2010 9:25:00 AM irrespective of what value is chosen from the DateTimePicker for second. I'm using the following statement but it's getting failed during the format conversion while storing it in the DateTime variable generating an error.

Dim dtmReminderTime As DateTime
dtmReminderTime = DateTime.Parse(Format(dtpReminderTime.Value, "MM/dd/yyyy hh:mm") + ":00 " + Format(dtpReminderTime.Value, "tt"))

I tried the other way but failed:
dtmReminderTime = Convert.ToDateTime(Format(dtpReminderTime.Value, "MM/dd/yyyy hh:mm") + ":00 " + Format(dtpReminderTime.Value, "tt"))

I have also tried using culture specific information but of no avail. Please help.
May 7 '10 #1

✓ answered by tlhintoq

Don't try parsing it... That's just a mess. Keep it as a DateTime object.
Get the DateTime.seconds property. Then add a negative number of seconds to it. That will be :00

Suedocode - you'll have to make the VB version of this.

myDateTimeVariable = 05/may/2010 at 14:50:23
myDateTimeVariable.AddSeconds(-1 * myDateTimeVariable.Seconds);

4 2234
tlhintoq
3,525 Expert 2GB
Don't try parsing it... That's just a mess. Keep it as a DateTime object.
Get the DateTime.seconds property. Then add a negative number of seconds to it. That will be :00

Suedocode - you'll have to make the VB version of this.

myDateTimeVariable = 05/may/2010 at 14:50:23
myDateTimeVariable.AddSeconds(-1 * myDateTimeVariable.Seconds);
May 7 '10 #2
Plater
7,872 Expert 4TB
Be sure to note that AddSeconds() (and similar functions) don't modify the active datetime object, but rather return a new one with the changes applied to it
May 10 '10 #3
priyamtheone
88 64KB
@tlhintoq
Optimum solution. Thanks.
May 11 '10 #4
Curtis Rutland
3,256 Expert 2GB
Be sure to note that AddSeconds() (and similar functions) don't modify the active datetime object, but rather return a new one with the changes applied to it
Very very true. I've spent hours chasing my tail not realizing why my code isn't working, only to later realize I was calling a method that returns its result rather than applying the result to the object that called it.

For the sake of completeness and to help anyone that comes across this post later, here is a corrected version of tlhintoq's example:

Expand|Select|Wrap|Line Numbers
  1. myDateTimeVariable = 05/may/2010 at 14:50:23
  2. myDateTimeVariable = myDateTimeVariable.AddSeconds(-1 * myDateTimeVariable.Seconds); 
May 12 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: KathyB | last post by:
I'm trying to concatenate fields in SQL stored proc for use in text field in asp.net dropdownlist. I'm running into a problem when I try to use a DateTime field, but can't find the answer (so far)...
9
by: news.microsoft.com | last post by:
Hello, I have DateTime property. How can I set this value to null?
4
by: scott cooper | last post by:
I hope this illustrates my question a little better. Consider the following script; why does the client side script change the div object's innerHtml property but the server side script does not?...
2
by: Tronex | last post by:
Hi there, I am desperate and need help on this from someone who actually knows what he does... cuz I certainly don't ;-) This is the situation: I have an ASP.NET web application (written in...
4
by: Arpan | last post by:
A SQL Server 2005 DB table has 4 columns namely "ID" (IDENTITY int column), "FirstName" (varchar(50)), "LastName" (varchar(50)) & "DOB" (datetime). Now when I am trying to add a new row using the...
15
by: mayurtandel | last post by:
i have problem whith store datetime value to database table the my system date formate is dd/mm/yyyy the date is store in mm/dd/yyyy formate what is the problem my code is as follow Public...
2
prabunewindia
by: prabunewindia | last post by:
hi friend, i want to set current dateTime to the registry. any one help me to do that. its taking only string value. when i tried with DateTime.Now.ToString() its display only the string(thats "...
3
by: David | last post by:
Hi all, I am using .NET 2 I have to work with short dates, e.g. 23 April 2008 will come to me as 23/04/08 I am splitting this value into int to put into a DateTime type... DateTime...
10
by: Newbie | last post by:
howdy... i am trying to execute a statement where i insert a record with datetime values into a sql database. Dim sqlcmd As New SqlCommand sqlcmd.CommandText = "update tbl_event set...
4
by: priyamtheone | last post by:
I'm trying to match a certain DateTime value with the current system DateTime. I have a timer and a label on a form. Timer interval is set to 1000. When the form loads, the timer starts ticking. As...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.