473,769 Members | 7,375 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

a question regarding DateTime

Hey

..NET 2.0

I'm about to create 2 input parameters for a method. These are 2 DateTime
parameters - one named "from" and another named "to"... - from and to
DateTime values...

Okay the trick is that the "from" DateTime should be like this
01.<MM>.<YYYY ss:mm.hh which means that today is the 03.12.2007 then the
"from" date should be 01.12.2007 00:00:00. What is the best way of
calculating this?

any suggestions?
Dec 3 '07 #1
10 1494
Jeff <do***@spam.mew rote:
.NET 2.0

I'm about to create 2 input parameters for a method. These are 2 DateTime
parameters - one named "from" and another named "to"... - from and to
DateTime values...

Okay the trick is that the "from" DateTime should be like this
01.<MM>.<YYYY ss:mm.hh which means that today is the 03.12.2007 then the
"from" date should be 01.12.2007 00:00:00. What is the best way of
calculating this?
DateTime values don't have any inherent formatting - it's only when you
convert them to and from strings that they're formatted.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
Dec 3 '07 #2
Okay, I've testing this solution now:

DateTime from
from = new DateTime(DateTi me.Now.Year, DateTime.Now.Mo nth, 1, 0, 0, 0)

I'm debugging the code and it seems like it is working. Do you see any
problems using this code?.
Dec 3 '07 #3
Jeff <do***@spam.mew rote:
Okay, I've testing this solution now:

DateTime from
from = new DateTime(DateTi me.Now.Year, DateTime.Now.Mo nth, 1, 0, 0, 0)

I'm debugging the code and it seems like it is working. Do you see any
problems using this code?.
Yes - you're calling DateTime.Now twice. I know it's unlikely, but
consider the case where it's December 31st, 23:59:59.999 - you call
DateTime.Now and the year is 2007. Then the clock times, and then you
call DateTime.Now and the month is January. Now you neither have Dec
2007 nor Jan 2008 - you have Jan 2007.

Use

DateTime now = DateTime.Now;
from = new DateTime(now.Ye ar, now.Month, 1, 0, 0, 0)

(Are you sure you don't want to be using UTC, by the way? I personally
try to keep to UTC whenever I can until the presentation layer, but I
don't know enough about what you're doing to say for sure. Just
something to think about.)

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
Dec 3 '07 #4
What about this approach:

DateTime date = DateTime.Now;
DateTime from
from = new DateTime(date.Y ear, date.Month, 1, 0, 0, 0)
Dec 3 '07 #5
Jeff <do***@spam.mew rote:
What about this approach:

DateTime date = DateTime.Now;
DateTime from
from = new DateTime(date.Y ear, date.Month, 1, 0, 0, 0)
Isn't that exactly what I put, just having changed the name of the
variable from "now" to "date"?

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
Dec 3 '07 #6
On Mon, 03 Dec 2007 05:15:24 -0800, Jon Skeet [C# MVP] <sk***@pobox.co m>
wrote:
Jeff <do***@spam.mew rote:
>What about this approach:

DateTime date = DateTime.Now;
DateTime from
from = new DateTime(date.Y ear, date.Month, 1, 0, 0, 0)

Isn't that exactly what I put, just having changed the name of the
variable from "now" to "date"?
Logically, sure. But he fixed one of your errors (missing variable
declaration) and introduced a new instance of the other one (missing
semi-colon). Surely that counts for something. :) :)
Dec 3 '07 #7
Liz

"Jeff" <do***@spam.mew rote in message
news:uB******** ******@TK2MSFTN GP05.phx.gbl...
Hey

.NET 2.0

I'm about to create 2 input parameters for a method. These are 2 DateTime
parameters - one named "from" and another named "to"... - from and to
DateTime values...

Okay the trick is that the "from" DateTime should be like this
01.<MM>.<YYYY ss:mm.hh which means that today is the 03.12.2007 then the
"from" date should be 01.12.2007 00:00:00. What is the best way of
calculating this?

any suggestions?
so you want the current month.year, always prefixed with "01" and postfixed
with "00:00:00" .. ? this should work:

DateTime dt = DateTime.Now;
string t = "01." + dt.Month.ToStri ng().PadLeft(2, '0') + "." +
dt.Year.ToStrin g() + " 00:00:00";
DateTime dt2 = Convert.ToDateT ime(t);
don't know if it's the "best" way ... it's a way ...


Dec 3 '07 #8
Liz wrote:
"Jeff" <do***@spam.mew rote in message
news:uB******** ******@TK2MSFTN GP05.phx.gbl...
>Hey

.NET 2.0

I'm about to create 2 input parameters for a method. These are 2 DateTime
parameters - one named "from" and another named "to"... - from and to
DateTime values...

Okay the trick is that the "from" DateTime should be like this
01.<MM>.<YYY Y ss:mm.hh which means that today is the 03.12.2007 then the
"from" date should be 01.12.2007 00:00:00. What is the best way of
calculating this?

any suggestions?

so you want the current month.year, always prefixed with "01" and postfixed
with "00:00:00" .. ? this should work:

DateTime dt = DateTime.Now;
string t = "01." + dt.Month.ToStri ng().PadLeft(2, '0') + "." +
dt.Year.ToStrin g() + " 00:00:00";
DateTime dt2 = Convert.ToDateT ime(t);
don't know if it's the "best" way ... it's a way ...

That's what I call a solutionproblem , a solution with more problems than
it solves.

Go with the way that Jon posted.

--
Lasse Vågsæther Karlsen
mailto:la***@vk arlsen.no
http://presentationmode.blogspot.com/
Dec 6 '07 #9
Liz

"Lasse Vågsæther Karlsen" <la***@vkarlsen .nowrote in message
news:uk******** ******@TK2MSFTN GP03.phx.gbl...
Liz wrote:
>so you want the current month.year, always prefixed with "01" and
postfixed with "00:00:00" .. ? this should work:

DateTime dt = DateTime.Now;
string t = "01." + dt.Month.ToStri ng().PadLeft(2, '0') + "." +
dt.Year.ToStrin g() + " 00:00:00";
DateTime dt2 = Convert.ToDateT ime(t);
don't know if it's the "best" way ... it's a way ...
That's what I call a solutionproblem , a solution with more problems than
it solves.

Go with the way that Jon posted.
I had not seen Jon's solution; now that I have, no doubt it's cleaner and I
would use it; compared to his, I see awkward, not problematic .. it does
work ... what "problems" do you see with this one other than it's slower and
more verbose?

Dec 7 '07 #10

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

Similar topics

4
2530
by: Max M | last post by:
# -*- coding: latin-1 -*- """ I am currently using the datetime package, but I find that the design is oddly asymmetric. I would like to know why. Or perhaps I have misunderstood how it should be used? I can make a datetime easily enough
15
14287
by: Fritz Switzer | last post by:
I'd like to have a string assigned the value of a DateTime.AddMinutes(amount) so that the string is formatted in "HH:MM" format. For example: DateTime.Now.AddMinutes(30) returns "00:30" DateTime.Now.AddMinutes(90) returns "1:30" or "01:30"
6
8991
by: Ante Perkovic | last post by:
Hi, How to declare datetime object and set it to my birthday, first or last day of this month or any other date. I can't find any examples in VS.NET help! BTW, what is the difference between date and datetime classes? Please, help
11
7250
by: Cor Ligthert | last post by:
Hello everybody, Jay and Herfried are telling me every time when I use CDate that using the datetime.parseexact is always the best way to do String to datetime conversions. They don't tell why only that I have to listen to them because they know it better. They told also that in a business situation it is better to use datetime.parseexact for changing cultures and not to use the globalization setting. I did not give them this sample,...
9
4928
by: Phil B | last post by:
I am having a problem with a datetime from a web services provider The provider is sending the following SOAP response <?xml version="1.0" encoding="utf-8"?> <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tns="urn:JadeWebServices/WebServiceProvider/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
0
856
by: Striker | last post by:
Hello All I am using asp.net(vbscripts) and sqlserver I am having a problem regarding time comparison, In my application a from field submits datetime in sqlsever database field, now what i need is that to compare that inserted (time only part of the database field) with the new datetime form field record and if the value of the new record is less in time than the value in the database then, i have to submit a new record in the database,...
4
1604
by: bsonline | last post by:
I hv a datetime format like '24/01/2008 15:56:01' . Now I have to select dates from a date range, but input datetime format like 24/1/2008 15:56:01'. I hv written the query like : "select date_column from date_table where to_char(date_column,'dd/mm/yyyy hh24:mi:ss') between input_date and input_date; " here I face a problem that when the date range within a month, it works fine. but when it become greater may be 2 or 6 month date range...
0
16509
yasirmturk
by: yasirmturk | last post by:
Standard Date and Time Functions The essential date and time functions that every SQL Server database should have to ensure that you can easily manipulate dates and times without the need for any formatting considerations at all. They are simple, easy, and brief and you should use them any time you need to incorporate any date literals or date math in your T-SQL code. create function DateOnly(@DateTime DateTime) -- Returns @DateTime...
1
2325
by: subhalalitha | last post by:
There is a requirement in our system. There is one Transaction Monitoring thread which wakes up after every 30 sec and checks for the requests that didnot get responses for "2 min" and sends Timeout Responses to the caller. There is a need to wake up this Thread upon an "event" to send Timeout responses for all the Requests. I tried samples and this combination does not seem to work for me. In short , The thread should wake up...
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
8873
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
7413
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
5309
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...
0
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3965
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
3565
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.