3

I am facing problem while converting DataTime into Time on 12 hour clock machine. Following code works fine on 24 Hour clock machine.

(new DisplayReminder(_name, _displayText, _snoozTime, TimeSpan.Parse(_startTime.ToShortTimeString(), CultureInfo.InvariantCulture))).Show();

TimeSpan.Parse(_startTime.ToShortTimeString() throws exeception that input string not in correct format, here I am trying to get time part from DateTime value _startDate Any suggestion or solution on this problem.

1
  • It's probably because of the AM/PM text, which is not really applicable in a timespan I suppose.
    – Patrick
    Dec 17, 2013 at 9:52

4 Answers 4

3

It's not clear what you're trying to do, but just getting the time of day shouldn't involve string conversions:

TimeSpan time = _stateTime.TimeOfDay;

I'd strongly advise you to avoid string conversions unless they're inherently part of what you're trying to achieve.

Personally I don't like using TimeSpan as a time of day anyway, but that's the BCL for you. You might want to also look into my Noda Time library which has a clearer separation of various date/time concepts.

2
  • @AnkushMadankar: Base Class Library. Basically ".NET libraries out of the box" (not strictly accurate, but close enough for now).
    – Jon Skeet
    Dec 17, 2013 at 9:58
  • Thanku again for knowledgeable replies, I check out Noda Times library, download it and use it but I am unable to use it exactly what is meant to be. But this solution works fine. Dec 17, 2013 at 10:47
1

Try this:

TimeSpan.ParseExact(
         _startTime.ToString("hh:mm:ss"), "hh:mm:ss",
                          System.Globalization.CultureInfo.InvariantCulture);

it will format your date to same format as TimeSpan.ParseExact accepts, so it will run on any machine (I assume that _startTime is DateTime)

1

Try

startTime.TimeOfDay.ToString()
1
  • 1
    What? You already have a timespan from TimeOfDay, why convert it to string and then convert it back, only to convert it to a string again?
    – Patrick
    Dec 17, 2013 at 10:15
0

I am pretty sure that it works fine when 24H but crushes in 12H system, BECAUSE of the "PM" "AM" part ! get rid of it you'll be safe 11:54:33 PM and 23:54:33 are VERY different in terms of handling

EDIT it might be not the best solution but it works if you cut the last indexes of the string:

    int index = _StartTime.IndexOf("M");
    if (index >= 0) 
{
_StartTime = _StartTime.Substring(0, index-1);
switch (_StartTime.IndexOf("P"))
case : -1
 _StartTime = _StartTime.Substring(0,_StartTime.Length);
 break;

default:
 string hours = _startTime.Substring(_StartTime.Length-8,2);
 int H = Convert.ToInt32(hours);
 H += 12;
 string result = _StartTime.Substring(0, _StartTime.Length-8)+ Convert.ToString(H)+_startTime.Substring(_StartTime.Length-6);

_StartTime = result;
break;
}

this will throw AM/PM away, you can use the same previous code after this SECOND EDIT it's the most horrible solution but it works ;)

14
  • Ok, I got problem now, can u plz suggest solution on it? Dec 17, 2013 at 9:59
  • @AnkushMadankar my answer allows you to parse AM/PM timespans Dec 17, 2013 at 10:02
  • The other solutions are far better. Not only is this error prone if AM/PM is missing, you will actually get the incorrect time back if you just cut of the last part
    – Patrick
    Dec 17, 2013 at 10:10
  • @chouaib System time now coming {29-11-2013 PM 12:59:00} so this wont work. Dec 17, 2013 at 10:11
  • 1
    yes @Patrick actually i edited it couple of times ;) my i idea is just to get rid of the AM/PM part no matter how to do it, i thought it doesn't matter but i will edit it again ;) ... wait
    – chouaib
    Dec 17, 2013 at 10:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.