473,796 Members | 2,460 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

DateTime manipulation

Say I have three DateTime variables:

DateTime A;
DateTime B;
DateTime C;

How can I assign to C just the date part of A and just the time part of B?

Something like this...

C = A.Date + B.TimeOfDay;

Except that isn't it.
Nov 16 '05 #1
7 10945
Patrick B <ne*******@devz oo.com> wrote:
Say I have three DateTime variables:

DateTime A;
DateTime B;
DateTime C;

How can I assign to C just the date part of A and just the time part of B?

Something like this...

C = A.Date + B.TimeOfDay;

Except that isn't it.


I ended up writing a utility method which did something like this -
it's not hard to write once and then use repeatedly. Basically use the
form of the DateTime constructor which takes everything you want, and
take some parts from A and some parts from B.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
Hi,

Another solution could be take the data part of A

a.Date

and the time components of B

b.Second , b.Hour , b.Minute

That should work

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
Patrick B <ne*******@devz oo.com> wrote:
Say I have three DateTime variables:

DateTime A;
DateTime B;
DateTime C;

How can I assign to C just the date part of A and just the time part of
B?

Something like this...

C = A.Date + B.TimeOfDay;

Except that isn't it.


I ended up writing a utility method which did something like this -
it's not hard to write once and then use repeatedly. Basically use the
form of the DateTime constructor which takes everything you want, and
take some parts from A and some parts from B.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #3
Or, if we wanted to be really nifty:

// Get the date and time.
DateTime now = DateTime.Now();

// Get the date portion, and the time.
DateTime date = now.Date;
TimeSpan time = now.TimeOfDay;

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Ignacio Machin ( .NET/ C# MVP )" <ignacio.mach in AT dot.state.fl.us > wrote
in message news:%2******** ********@TK2MSF TNGP12.phx.gbl. ..
Hi,

Another solution could be take the data part of A

a.Date

and the time components of B

b.Second , b.Hour , b.Minute

That should work

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
Patrick B <ne*******@devz oo.com> wrote:
Say I have three DateTime variables:

DateTime A;
DateTime B;
DateTime C;

How can I assign to C just the date part of A and just the time part of
B?

Something like this...

C = A.Date + B.TimeOfDay;

Except that isn't it.


I ended up writing a utility method which did something like this -
it's not hard to write once and then use repeatedly. Basically use the
form of the DateTime constructor which takes everything you want, and
take some parts from A and some parts from B.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


Nov 16 '05 #4
Ahh, I get you...

DateTime C = new DateTime(A.Year , A.Month, A.Day, B.Hour, B.Minute,
B.Second);

Thanks, that works.
Nov 16 '05 #5
Patrick B wrote:
How can I assign to C just the date part of A and just the time part of B?

Use the DateTime constructor with Year, Month and Day from DateTime a
and Hour, Minute and Second from DateTime b.

DateTime a=new DateTime(2005,0 1,04);
DateTime b=DateTime.Now;
DateTime c=new DateTime(a.Year ,a.Month,a.Day, b.Hour,b.Minute ,b.Second);

Anders Norås
http://dotnetjunkies.com/weblog/anoras/
Nov 16 '05 #6
Patrick B <ne*******@devz oo.com> wrote:
Ahh, I get you...

DateTime C = new DateTime(A.Year , A.Month, A.Day, B.Hour, B.Minute,
B.Second);

Thanks, that works.


Goodo - but actually, looking at your original post, I don't see why
that shouldn't work. For instance:

using System;

class Test
{
static void Main()
{
DateTime now = DateTime.Now;
DateTime yesterday = DateTime.Today. AddDays(-1);
DateTime thisTimeYesterd ay = yesterday.Date+ now.TimeOfDay;

Console.WriteLi ne(thisTimeYest erday);
}
}

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #7
Try This

DateTime A = DateTime.Now;

DateTime B = DateTime.Now.Ad d(-1);

DateTime C = A.Date.AddTicks (B.TimeOfDay.Ti cks);

Ciaran

"Patrick B" <ne*******@devz oo.com> wrote in message
news:ui******** ******@TK2MSFTN GP09.phx.gbl...
Say I have three DateTime variables:

DateTime A;
DateTime B;
DateTime C;

How can I assign to C just the date part of A and just the time part of B?

Something like this...

C = A.Date + B.TimeOfDay;

Except that isn't it.

Nov 16 '05 #8

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

Similar topics

4
2352
by: Michele Simionato | last post by:
Strangely enough, I never needed the datetime and calendar module before, so I just looked at them today. I am surprised I don't easily find an interval function such this: import datetime def interval(day, lastday, step): # not necessarely restricted to days while day < lastday:
1
84131
by: Propel Exacto | last post by:
Hey guys, I am using MySQL 4.0.18 and I have a field named "order_datetime" in which I store data in the format 20041001 23:00:00 (for example Oct 1, 2004 11pm) When I do a select statement to find dates in a given range, the result set never includes records that have the ending date. For example, if my SQL statement is
3
19765
by: thomasamillergoogle | last post by:
Hello, I have a table that unfortunatley has a field 'DateSold' with datatype of int that SHOULD be datetime. So, I am trying to do a cast/convert. The int is stored as 20040520 (which means 5/20/2004) I tried saying Select col1 from table where cast(DateSold as DATETIME) > '1/1/2004'
4
10802
by: Andrew Wilhite | last post by:
Hello, I am just learning C# and I have ran into a problem that I cannot seem to resolve. I need to capture today's date, subtract a day, and then put it into the MM-DD-YYYY format. I know how to do one or the other, but i cannot figure out how to do both. Here is my code snippet: DateTime Date1 = System.DateTime.Now.AddDays(-1); string Day = System.Convert.ToString(Date1.Day);
26
2689
by: Reny J Joseph Thuthikattu | last post by:
Hi, I have a variabe in the format of 'DD-MON-YYYY HH:MI AM' .I want to add a miniute to it.How can i do that? by manipulation i want to make '01-JUNE-2004 11:59 PM' to '02-JUNE-2004 12:00 AM' How do i do that? Reny ---
3
2490
by: Tim Cowan | last post by:
Hi, In my application I need to compare whether one time is greater than another. What I am doing right now is taking the current datetime, formatting as string without the time element, and then appending the time I want to the date and converting to date. DateTime dOpen = System.Convert.ToDateTime(now.Date.ToString("MMM dd, yyyy") + " " + storeOpen.Hour + ":" + storeOpen.Minute);
2
1354
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
1665
by: almurph | last post by:
Hi, Hope that you can help me with this. I'm calling DateTime.Now and storing in a DateTime container. I see that the pattern format is: m/ dd/yyyy I need it to be in the format: yyyy-MM-dd HH:MM:SS as type DateTime but I don't know how to do this! Can anyone help me please? Any comments/advice/suggestions/code-sampels much appreciate.
2
1658
by: csharpula csharp | last post by:
Hello, I am trying to add Date and Current time to a name of a log file. I tried doing this: string logDateExtention = DateTime.Now.ToShortDateString().ToString() + DateTime.Now.ToShortTimeString().ToString(); string logName = Data.Name + Data.ProductName + logDateExtention; And this throws exception during the debug that the name is not legal
0
9683
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
10457
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
10231
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...
1
10176
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
10013
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
7550
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
5443
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4119
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
2
3733
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.