473,614 Members | 2,335 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to extract time only from DateTime.Now, and compare it with

I have code below. But it won't compile.
Error:

Cannot implicitly convert type 'System.TimeSpa n' to 'System.DateTim e'

DateTime now = DateTime.Now.Ti meOfDay;

DateTime openTime = Convert.ToDateT ime("9:30:00");

if (now < openTime)
{
// Do nothing if it's before 9:30 AM
return;
}
Oct 21 '08 #1
3 15973
"Curious" <fi********@yah oo.comwrote in message
news:c9******** *************** ***********@m32 g2000hsf.google groups.com...
>I have code below. But it won't compile.
Error:

Cannot implicitly convert type 'System.TimeSpa n' to 'System.DateTim e'

DateTime now = DateTime.Now.Ti meOfDay;

DateTime openTime = Convert.ToDateT ime("9:30:00");

if (now < openTime)
{
// Do nothing if it's before 9:30 AM
return;
}
TimeOfDay returns a TimeSpan and you're assigning it to a DateTime. Try:

TimeSpan now = DateTime.Now.Ti meOfDay;

TimeSpan openTime = TimeSpan.Parse( "0.09:30:00 ");

if (now < openTime)
{
return;
}

Oct 21 '08 #2
Thanks John!

It works! Could you tell me why you use TimeSpan.Parse( "0.09:30:00 ")
instead of TimeSpan.Parse( "9:30:00") ?

Oct 22 '08 #3
"Curious" <fi********@yah oo.comwrote in message
news:c4******** *************** ***********@v30 g2000hsa.google groups.com...
Thanks John!

It works! Could you tell me why you use TimeSpan.Parse( "0.09:30:00 ")
instead of TimeSpan.Parse( "9:30:00") ?
The leading "0." is the number of days in the TimeSpan, I just included it
to have a complete specification.

Oct 23 '08 #4

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

Similar topics

9
18062
by: scott | last post by:
I have a field with datetime values like below LISTING 1. Can someone help me write code strip the time part so only values like "7/15/2005" will be left. Note - We must be able to strip dates with double digit months and days, so i can't just use the right function with a hard coded parameter. LISTING 1
3
1804
by: laurenq uantrell | last post by:
Is here an easy way to get 9:00 PM from 5/1/2005 9:00:00 PM without doing: Cast(DATEPART(HOUR,xDate) as varchar(2)) + ':' + Cast(DATEPART(MINUTE,xDate) as varchar(2)) lq
1
2914
by: Tim::.. | last post by:
Hi, Can someone tell me what the best way to remove the time from a datetime field in a database? I know I could use LEN but I was just wondering if there is a better way to do it? E.G: 8/30/2004 12:00:00 AM Should return 8/30/2004
4
12903
by: kashifsulemani | last post by:
we have a table like this OrderNo OrderDate 1 2005-11-04 01:12:47.000 2 2005-11-19 04:26:54.000 3 2005-11-16 11:03:23.000 4 2005-11-21 15:58:37.000 5 2005-11-24 21:45:04.000
0
1170
by: savvy | last post by:
I am developing a Recruitment Agency Website I am using MS SQL Server in which I have a table called CV_Details which stores all the details of different Job Seekers. I have stored all the Word and PDF docs of the Job seekers in a 'mycatalog' using Index Server and stored the path in the CV_Details table along with rest of the cv details. Basically I want to download the cv's which i get after making a search in the doc's. I am able to get...
2
1347
by: YMPN | last post by:
Hello Guys, I have a datetime coloumn, i want to extract time only this coloumn and save this time only to another coloumn in the same table. I am new to asp.net.. thanks..
2
1522
by: newUser1234 | last post by:
I've been trying to extract time from excel sheet cell to a vb text box, instead of giving me the time it gives me a double type number like "0.4375", in the excel file that cell has the value 10:30 AM. the code I'm using to extract it is as follows: TextBox.Text = xlSheet.Cells(rowNum, 13).Value This code will extract all other types of values from the excel sheet cells i have except time, I don't know why. Any help would be greatly...
2
3367
by: Sreenivas | last post by:
Hi every one, I am new to sql server2005,I have datetime column in my database ..I need to display only time part of the datetime .I need to display in AM/PM format ..datetime is stored in this format mm/ dd/yyyy hh:mm:ss AM/PM .I need to display in this format hh:mm:ss AM/ PM ,How can i do that?? I searched for this on net but i got 24hour format . i tried this on my own ,but no hope! Thanks in advance,
1
3008
by: jrhitokiri | last post by:
I just want to extract the time component of the calendar object for comparison to another. Is this possible? here's my code: try { String str_date="2009-10-15 12:1:00"; DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd k:mm:ss"); Date date = (Date)formatter.parse(str_date); Calendar cal = Calendar.getInstance();
5
1846
by: Rootz88 | last post by:
ok I have this string and other similar( ull find them in the attachment) "BRUNEI BANDAR SERI BEGAWAN 5770 343653" I need to extract BRUNEI, BANDAR SERI BEGAWAN, 5770 and 343653 but i cant find a way to do it that work for all my string that i have.. Can someone help me on that plz.. thx
0
8142
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
8640
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...
0
8589
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8443
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
6093
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
5548
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();...
0
4136
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2573
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1438
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.