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

DateTime Problem

Hi there
I am trying to get a single digit hour in 12-hour format with the
following code.

DateTime hr = DateTime.Now;
string hour = hr.ToString("h");

However, I get "Input string was not in a correct format" error. Please
help me to correct this problem.

Many Thanks
Niju

Nov 19 '05 #1
5 1666
niju <di**************@gmail.com> ha scritto:
Hi there
I am trying to get a single digit hour in 12-hour format with the
following code.

DateTime hr = DateTime.Now;
string hour = hr.ToString("h");

However, I get "Input string was not in a correct format" error.
Please help me to correct this problem.
string hout=hr.ToString("hh");

Many Thanks
Niju


--
AZ [Microsoft - .NET MVP]
Mia Home page: http://ciclismo.sitiasp.it
Asp.Net community: http://www.aspitalia.com
Il mio blog: http://blogs.aspitalia.com/az
Nov 19 '05 #2
Hi Niju,

How does the date that you are passing to the function? Maybe you have to
first use DateTime.Parse() to get it into a format the runtime understands
and than extract your desired format.
Let me know if it helped you or not...

Cheers,
Tom Pester
Hi there
I am trying to get a single digit hour in 12-hour format with the
following code.
DateTime hr = DateTime.Now;
string hour = hr.ToString("h");
However, I get "Input string was not in a correct format" error.
Please help me to correct this problem.

Many Thanks
Niju

Nov 19 '05 #3
Thanks Tom

I have no problem getting hour in 24-hour format with the
following code.

DateTime hr = DateTime.Now;
string hour = hr.ToString("hh");

However i cannot get a single digit hour in 12-hour format with the
following code.

DateTime hr = DateTime.Now;
string hour = hr.ToString("h");

Nov 19 '05 #4
Hi niju,

I found the cause of the error :

"This is actually by design. A single character is interpreted as a Standard
DataTime format, "H" & "h"
are not standard DateTime format strings. Hence those two cause exceptions.
A Custom DateTime format needs to be at least 2 characters."

This extract is taken from http://groups.google.com/group/micro...ea90ec4c52d91a

Try this code, it should work :

DateTime hr = new DateTime(2005,5,5,15,13,1);
string hour = hr.ToString("hh");
string hourTrimmed = hr.ToString("hh").TrimStart('0');

Response.Write(hour + "<br>" + hourTrimmed);

Let me know if it helped you or not...

Cheers,
Tom Pester
Thanks Tom

I have no problem getting hour in 24-hour format with the following
code.

DateTime hr = DateTime.Now;
string hour = hr.ToString("hh");
However i cannot get a single digit hour in 12-hour format with the
following code.

DateTime hr = DateTime.Now; string hour = hr.ToString("h");

Nov 19 '05 #5
Thanks Tom that helped.

Nov 19 '05 #6

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

Similar topics

4
by: Mingus Tsai | last post by:
Hello- please help with unpickling problem: I am using Python version 2.3.4 with IDLE version 1.0.3 on a Windows XPhome system. My problem is with using cPickle to deserialize my pickled...
2
by: Rey | last post by:
Howdy all. My problem deals w/inserting nulls into database (SQL Svr 2K) for the datetime fields activityDate and followUpDate where nulls are allowed. >From the web form, the user can type...
8
by: Alan Silver | last post by:
Hello, I have a custom control that displays the date and time. I currently have properties for the day, month, year, hour and minute, which have to be set separately. This is inefficient. I...
5
by: I am Sam | last post by:
I have created this DateTime object and instanced it I think correctly DateTime myClubNow1=new...
9
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...
3
by: pranesh.nayak | last post by:
I'm facing an error:"String was not recognized as a valid DateTime" whille accessing DateTime from webservice. And when I try to set DateTime to the same webservice it fails with error:"Date...
6
by: JFieseler | last post by:
Hi all, in a huge project i have the following problem. I create an object which contains many private members (i know that this is not correct, but it is a single use migration program). The...
5
by: iulian.ilea | last post by:
Is correct to have a varchar field and insert dates of type dd/mm/yyyy into it? I choose this method because I have an application that runs on more than one server. So, if I used a datetime field...
4
by: =?Utf-8?B?QmFidU1hbg==?= | last post by:
Hi, I have a GridView and a SqlDataSource controls on a page. The SqlDataSource object uses stored procedures to do the CRUD operations. The DataSource has three columns one of which -...
4
by: =?Utf-8?B?c2lwcHl1Y29ubg==?= | last post by:
Hi I am having a problem formatting a string when the time is in format hh.mm.ss - used in Europe Parse seems ok when the date uses "/" or "." as seperator but I get an exception when time...
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?
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
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
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...
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,...
0
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...

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.