473,404 Members | 2,137 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,404 software developers and data experts.

DateTime & string format question

Hello, I am new to C# and cannot figure out how to do something simple. How
can I build two strings out of a DateTime object? I need one string to
contain the DateTime's date in YYMMDD format. The other string should
contain the DateTime's time in HHMMSS format. Both strings should always
return a length of 6 characters (they should have leading zero if neccessary
to pad each field out to two digits). There should be no delimeter
characters in the strings, just the digits. e.g: "040104" for 1/4/2004,
"135259" for 1:52:59 PM. Any advice will be appreciated.
Nov 15 '05 #1
4 8238
http://msdn.microsoft.com/library/de...tringtopic.asp
has details about the ToString() method of the DateTime object.

To get the date, you could use:
string myDate = myDateTimeObject.ToString("d").Replace("/", "");

To get the time, you could use:
string myTime = myDateTimeObject.ToString("T").Replace(":", "");

Hope this helps,

Mun

--
Munsifali Rashid
http://www.munsplace.com/

"Trevor Balcom" <tb*****@attbi.co> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Hello, I am new to C# and cannot figure out how to do something simple. How can I build two strings out of a DateTime object? I need one string to
contain the DateTime's date in YYMMDD format. The other string should
contain the DateTime's time in HHMMSS format. Both strings should always
return a length of 6 characters (they should have leading zero if neccessary to pad each field out to two digits). There should be no delimeter
characters in the strings, just the digits. e.g: "040104" for 1/4/2004,
"135259" for 1:52:59 PM. Any advice will be appreciated.

Nov 15 '05 #2
See if this helps:
string s1 = DateTime.Now.ToString("yyMMdd");
string s2 = DateTime.Now.ToString("hhmmss");
HTH
Brian W

"Trevor Balcom" <tb*****@attbi.co> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Hello, I am new to C# and cannot figure out how to do something simple. How can I build two strings out of a DateTime object? I need one string to
contain the DateTime's date in YYMMDD format. The other string should
contain the DateTime's time in HHMMSS format. Both strings should always
return a length of 6 characters (they should have leading zero if neccessary to pad each field out to two digits). There should be no delimeter
characters in the strings, just the digits. e.g: "040104" for 1/4/2004,
"135259" for 1:52:59 PM. Any advice will be appreciated.

Nov 15 '05 #3
Be very careful. The formats you chose (short date for example) are subject
to regional formatting rules. For example, in the U.S., March 5 would yeild
"3/5" whereas in most European countries you would get "5/3". If Trevor is
looking for specific placement of those digits, then I would suggest a more
literal format string (like the ones in Brian's post).

-Rob Teixeira [MVP]

"Munsifali Rashid" <mun.news@#RemoveToReply#cordlessmouse.co.uk> wrote in
message news:OB**************@TK2MSFTNGP11.phx.gbl...
http://msdn.microsoft.com/library/de...tringtopic.asp has details about the ToString() method of the DateTime object.

To get the date, you could use:
string myDate = myDateTimeObject.ToString("d").Replace("/", "");

To get the time, you could use:
string myTime = myDateTimeObject.ToString("T").Replace(":", "");

Hope this helps,

Mun

--
Munsifali Rashid
http://www.munsplace.com/

Nov 15 '05 #4
Yep, good point. In this case, Brian's post would be a better solution.

Thanks for pointing that out Rob :-)

Cheers,

Mun
--
Munsifali Rashid
http://www.munsplace.com/


"Rob Teixeira [MVP]" <RobTeixeira@@msn.com> wrote in message
news:e0*************@TK2MSFTNGP10.phx.gbl...
Be very careful. The formats you chose (short date for example) are subject to regional formatting rules. For example, in the U.S., March 5 would yeild "3/5" whereas in most European countries you would get "5/3". If Trevor is
looking for specific placement of those digits, then I would suggest a more literal format string (like the ones in Brian's post).

-Rob Teixeira [MVP]


Nov 15 '05 #5

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

Similar topics

3
by: Ed | last post by:
Hi there, My problem is the following: When I assign a custom formatted Date & Time to a Date variable it loses it’s formatting. Ex. 2005-06-07 15:46 now when I assign this to a variable of...
6
by: Carl | last post by:
Hi, to be use with SQL server database 1- From window form textbox object From this assignement, txtTOTAL.Text="0.00m"; How do you convert back to decimal type ? How do you assign a...
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...
10
by: Kim Hellan | last post by:
I have a simple string in the format "DD-MM-YY hh:mm:ss", that I need to convert to a DateTime value. I know this is a standard problem, but please don't just link to all the MSDN pages regarding...
38
by: nobody | last post by:
I know that given a FormatString and a DateTime you can use DateTime.ToString(...) to convert the DateTime to a String. My question is how can you turn that around? Given a String and a...
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'...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...
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,...

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.