473,804 Members | 2,243 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 10946
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
84137
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
2693
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
1355
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
1659
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
9715
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
9595
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,...
1
10354
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
9175
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7642
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
6867
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();...
1
4313
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
3835
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3002
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.