473,418 Members | 3,431 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,418 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 2237
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.