473,385 Members | 1,341 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,385 software developers and data experts.

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 10914
Patrick B <ne*******@devzoo.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.com>
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.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Patrick B <ne*******@devzoo.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.com>
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.com

"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us> wrote
in message news:%2****************@TK2MSFTNGP12.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.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Patrick B <ne*******@devzoo.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.com>
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,01,04);
DateTime b=DateTime.Now;
DateTime c=new DateTime(a.Year,a.Month,a.Day,b.Hour,b.Minute,b.Se cond);

Anders Norås
http://dotnetjunkies.com/weblog/anoras/
Nov 16 '05 #6
Patrick B <ne*******@devzoo.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 thisTimeYesterday = yesterday.Date+now.TimeOfDay;

Console.WriteLine(thisTimeYesterday);
}
}

--
Jon Skeet - <sk***@pobox.com>
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.Add(-1);

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

Ciaran

"Patrick B" <ne*******@devzoo.com> wrote in message
news:ui**************@TK2MSFTNGP09.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
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 ...
1
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...
3
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...
4
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...
26
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'...
3
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...
2
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
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...
2
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() +...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...

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.