473,473 Members | 1,891 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

System.DateTime conversion

DWW
System.DateTime returns a string with the date and time. I want to convert
the time to an integer, for example, convert 10:24:45 to 102445. Much
research has turned up a myriad of variations on formatting using Parse and
DateTimeFormatInfo, but nothing that returns the time as an integer. Any
ideas?

Thanks.

DW
Nov 17 '05 #1
4 4472
DWW,

I don't think that this is the best way to get the time portion as an
integer. The reason being that you would have to convert it back to a
string before you wanted to convert it into a time span again.

Rather, I would do this:

static int TimeSpanToInt32(TimeSpan timeSpan)
{
// Get the return value, rounded down.
return (int) Math.Floor(timeSpan.TotalSeconds);
}

To convert back, you do this.

static TimeSpan Int32ToTimeSpan(int timeSpan)
{
// Convert to a timespan.
return TimeSpan.FromSeconds(timeSpan);
}

Note that there will be some loss in the conversion here.

If you really want to just create an integer based on that string, it's
going to be a little harder, since you can't specify the format of the
timespan when calling ToString (and it expects a set format when it is
passed back as well).

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"DWW" <al*@jsnospam.com> wrote in message
news:V9********************@giganews.com...
System.DateTime returns a string with the date and time. I want to
convert the time to an integer, for example, convert 10:24:45 to 102445.
Much research has turned up a myriad of variations on formatting using
Parse and DateTimeFormatInfo, but nothing that returns the time as an
integer. Any ideas?

Thanks.

DW

Nov 17 '05 #2
DWW,
The framework doesn't have any functions that return an integer formatted as
102445 per se.

What I do is write a simply function to convert the Date Time. Something
like:

DateTime dt = ...;
int hms = dt.Hour * 10000 + dt.Minute * 100 + dt.Second;

--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"DWW" <al*@jsnospam.com> wrote in message
news:V9********************@giganews.com...
| System.DateTime returns a string with the date and time. I want to
convert
| the time to an integer, for example, convert 10:24:45 to 102445. Much
| research has turned up a myriad of variations on formatting using Parse
and
| DateTimeFormatInfo, but nothing that returns the time as an integer. Any
| ideas?
|
| Thanks.
|
| DW
|
|
Nov 17 '05 #3
DWW
Thanks, Jay. Shortly after I posted this, I figured it out and I did it the
way you present below. Unfortunately, MS's documentation is a little spare
on this issue, but it works. (My implementation multiplies the
DateTime.Hour by 100 and adds DateTime.Minute.)

Thanks.

DW

"Jay B. Harlow [MVP - Outlook]" <Ja************@tsbradley.net> wrote in
message news:%2****************@tk2msftngp13.phx.gbl...
DWW,
The framework doesn't have any functions that return an integer formatted
as
102445 per se.

What I do is write a simply function to convert the Date Time. Something
like:

DateTime dt = ...;
int hms = dt.Hour * 10000 + dt.Minute * 100 + dt.Second;

--
Hope this helps
Jay [MVP - Outlook]
.NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"DWW" <al*@jsnospam.com> wrote in message
news:V9********************@giganews.com...
| System.DateTime returns a string with the date and time. I want to
convert
| the time to an integer, for example, convert 10:24:45 to 102445. Much
| research has turned up a myriad of variations on formatting using Parse
and
| DateTimeFormatInfo, but nothing that returns the time as an integer.
Any
| ideas?
|
| Thanks.
|
| DW
|
|

Nov 17 '05 #4
DWW <al*@jsnospam.com> wrote:
Thanks, Jay. Shortly after I posted this, I figured it out and I did it the
way you present below. Unfortunately, MS's documentation is a little spare
on this issue, but it works. (My implementation multiplies the
DateTime.Hour by 100 and adds DateTime.Minute.)


What do you want to be documented which isn't, out of interest? If you
look for custom date and time format strings, you should find enough
information - I've never found it to be a problem.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 17 '05 #5

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

Similar topics

4
by: GiriT | last post by:
Would appreciate some insight into how people are dealing with the implicit conversion of timezones that .NET does. If a server in one timezone delivers up a typed dataset to a component in...
3
by: Christopher Crooker | last post by:
We had a function with the signature: Function(Object *, DateTime, DateTime, EnumType) and then changed the class some and there was a 4 parameter function with the signature: ...
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'...
1
by: Untitled | last post by:
I have an application that stores all DateTime inforamtion in UTC. However, this application will be accessed from arround the world. I would like my users to be able to specify their prefered...
7
by: groups | last post by:
This is my first foray into writing a generic method and maybe I've bitten off more than I can chew. My intent is to have a generic method that accepts a value name and that value will be...
9
by: Hrvoje Voda | last post by:
How to convert text from textbox "12.05.1977" into "12/05/1977"? and also check if the text is in right format before converting? Hrcko
2
by: Mike | last post by:
Hi, I have DateTime filed in db table. I'd like to search my db based on DateTime filed, actually to list all rows with particular date, for e.g. 16.11.2007... I send string to method for....
11
by: =?Utf-8?B?UGFvbG8=?= | last post by:
I have a SQL database table with rows in which the primary key is a DateTime type (although I don't use the Time part). Have added numerous rows and now want to delete one so I enter the date...
1
Manikgisl
by: Manikgisl | last post by:
But the problem is we have dates in Varchar instead Datetime While Converting Varchar To Datetime All four formats are unable to Convert ie select Convert(Datetime,'18-11-2008 2:35:19...
0
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,...
0
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,...
0
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...
0
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...
0
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,...
1
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...
0
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...
0
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 ...
1
muto222
php
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.