473,769 Members | 4,173 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Adding a string time value to a date variable

I have a standard .Net DateTime variable to which I want to add a time
value. The catch is that the time value is only readily available as a
string as "HH:mm".

Question is whether there's a simple construction that would do this.
I'm thinking of something along the lines of:

Dim MyDate as Date = New Date(1, 1, 2007)
Dim MyNewDate as Date
Dim MyTime as String = "HH:mm"

MyNewDate = Date.Parse(MyDa te.ToString("?? ") & " " & MyTime)

(Not sure what the ?? should be.)

or maybe

MyNewDate = Date.Parse(MyDa te.ToShortDateS tring & " " & MyTime)

might be the best option.

I know I could deconstruct the HH:mm string into its constituent hours
and minutes integer values, but I was just curious as to whether
there's a more direct way of doing this. Another alternative might be
if there's any way of parsing a Timespan type from the time string,
but I can't spot any such method.

NB This needs to work for locales using both US and non-US date
formats, which is partly why I'm asking here, ie to get a solution
that will definitely work worldwide and not just in my current locale.
JGD
Feb 3 '07 #1
2 8734
John,,

In my idea comes this the most near your question.

\\\
Dim MyDate As Date = New Date(2007, 1, 1)
Dim MyNewDate As Date
Dim MyTime As New TimeSpan(1, 30, 0)
MyNewDate = MyDate.Add(MyTi me)
///

I hope this helps,

Cor

"John Dann" <ne**@prodata.c o.ukschreef in bericht
news:g4******** *************** *********@4ax.c om...
>I have a standard .Net DateTime variable to which I want to add a time
value. The catch is that the time value is only readily available as a
string as "HH:mm".

Question is whether there's a simple construction that would do this.
I'm thinking of something along the lines of:

Dim MyDate as Date = New Date(1, 1, 2007)
Dim MyNewDate as Date
Dim MyTime as String = "HH:mm"

MyNewDate = Date.Parse(MyDa te.ToString("?? ") & " " & MyTime)

(Not sure what the ?? should be.)

or maybe

MyNewDate = Date.Parse(MyDa te.ToShortDateS tring & " " & MyTime)

might be the best option.

I know I could deconstruct the HH:mm string into its constituent hours
and minutes integer values, but I was just curious as to whether
there's a more direct way of doing this. Another alternative might be
if there's any way of parsing a Timespan type from the time string,
but I can't spot any such method.

NB This needs to work for locales using both US and non-US date
formats, which is partly why I'm asking here, ie to get a solution
that will definitely work worldwide and not just in my current locale.
JGD

Feb 3 '07 #2
If your time value (string) will ALWAYS be in HH:mm format, i.e., 5
characters long, 3rd character = ":", ist 2 characters are in the range "00"
to "23" and last 2 characters are in the range "00" to "59", then you can
use the following which will always work, regardless of culture:

Dim _d As DateTime = DateTime.ParseE xact(MyTime, "HH:mm", nothing)

Dim MyNewDate As DateTime =
MyDate.Date.Add Hours(_d.Hour). AddMinutes(_d.M inute)
"John Dann" <ne**@prodata.c o.ukwrote in message
news:g4******** *************** *********@4ax.c om...
>I have a standard .Net DateTime variable to which I want to add a time
value. The catch is that the time value is only readily available as a
string as "HH:mm".

Question is whether there's a simple construction that would do this.
I'm thinking of something along the lines of:

Dim MyDate as Date = New Date(1, 1, 2007)
Dim MyNewDate as Date
Dim MyTime as String = "HH:mm"

MyNewDate = Date.Parse(MyDa te.ToString("?? ") & " " & MyTime)

(Not sure what the ?? should be.)

or maybe

MyNewDate = Date.Parse(MyDa te.ToShortDateS tring & " " & MyTime)

might be the best option.

I know I could deconstruct the HH:mm string into its constituent hours
and minutes integer values, but I was just curious as to whether
there's a more direct way of doing this. Another alternative might be
if there's any way of parsing a Timespan type from the time string,
but I can't spot any such method.

NB This needs to work for locales using both US and non-US date
formats, which is partly why I'm asking here, ie to get a solution
that will definitely work worldwide and not just in my current locale.
JGD
Feb 3 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2842
by: jessie | last post by:
I use a chinese version of Windows XP. And excute a vb script code to get a date value from a Excel file. The data value is in English format in the Excel file. But I found the vb script converts it to chinese data format automatically. So I can not compare it with another data variable as I wish. So how can I force it to English date format? Thanks.
6
7060
by: Mike | last post by:
Dim vDateE As String vDateE = #7/12/2005# The following works to find Cost in TblExpenses for 7/12/2005: MsgBox (DSum("Cost", "TblExpenses", "DateofPurchase = #7/12/2005#")) However I have worked for hours to try and put a variable in place of #7/12/2005#
3
11731
by: Reney | last post by:
I am using Access Database in my program. The column in the table that I am going to use has date/time value with Medium Time selected. (HH:mm). The program is recording a clock in time to this field, which is the time when the entry is made. If you check the database, it shows the correct time in the correct format. But when you are calling it with a dataset and showing with a datagrid, it doesn't show the time value but it always shows...
3
18637
by: Liam Mac | last post by:
Hi All, Can anyone direct me or provide advice on how I can assign a null value to a date variable in vb.net. Basically what I'm doing is that I'm looping through a recordset where I have three date fields that may or maynot have date values, if no date values exists in the source field, the value is null. but when I pass a record from the recordset to SQL command parameter query for record insertion into a different table, the date...
6
5649
by: Aussie Rules | last post by:
Hi, I have a datepicker that show a calender. The user picks a date and the time component is always 00:00. I then have a drop down that provides a list of times, (10:00, 11:00 etc), and I want to combine this with the date value, so that I can store it in a single field in the database. How can I combine these two values into one ?
3
3543
by: raam | last post by:
Hello all, I am using Turbo c 2.1. I have reqritten the timer interrupt in PC as per my requirement.When I use gettime() and getdate() in my program leter I am not getting current time and date.How to get the Time and Date directly from the CMOS. Thanks in advance. Ram
7
4950
compman9902
by: compman9902 | last post by:
Hello, thank you for looking at this post. I need help with the date and time in a program. All I need to do is put the date and time into one variable and that would solve everything. When I say date and time, I mean something like "10/12/08,16:52:08". Thanks. Also, the variable it has to be put in is a string variable. Here are my specs.: Dev-C++ 4.9.9.2 Windows XP Service Pack 2 Thanks again.
17
3448
by: ginajohnst | last post by:
Hi All. I'm having a problem adding days to a date. My date is in the string format dd/mm/yyyy eg. 23/08/2007 in my form field. I can't work out how to add 50 days to that date and then write it to another form field.
5
10217
by: gubbachchi | last post by:
Hi all, How to pass the php date variable to javascript function. Here is the code I have tried out, but this is not recovering the date correctly in the javascript function <html> <script type="text/javascript"> function add($date1){ alert($date1)
0
9589
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10211
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9994
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9863
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7409
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6673
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
3562
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.