Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7887405
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T05:34:18+00:00 2026-06-03T05:34:18+00:00

I have this method: public static DateTime GetDatetime(string ampm, string hour, string minute) {

  • 0

I have this method:

public static DateTime GetDatetime(string ampm, string hour, string minute)
        {
            int iHour = Convert.ToInt32(hour);
            int iMinute = Convert.ToInt32(minute);

            if (ampm == "PM" && iHour != 12)
                iHour = 12 + iHour;

            DateTime dtTime = new DateTime(DateTime.Now.Year, DateTime.Now.Month,
                DateTime.Now.Day, iHour, iMinute, 0);

            return dtTime;
        }

which basically accepts AM/PM and hour and minute and gives DateTime. I give input as

   DateTime startTIme = GetDatetime("AM", "12", "30");

I get time correctly as 12:30 in morning on my local machine. However when I run this same method on server I get 12:30 PM. This is driving me nuts. Can anybody help me out? What am I doing wrong?

Update:

My new function is:

public static DateTime GetDatetime(string ampm, string hour, string minute)
        {
            int iHour = Convert.ToInt32(hour);
            int iMinute = Convert.ToInt32(minute);

            if (ampm == "PM" && iHour != 12)
                iHour = 12 + iHour;
            else if (ampm == "AM" && iHour == 12)
                iHour = 0;

            DateTime dtTime = new DateTime(DateTime.Now.Year, DateTime.Now.Month,
                DateTime.Now.Day, iHour, iMinute, 0);

            return dtTime;
        }

This seem to work fine. Can anybody find any issue in this code?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T05:34:19+00:00Added an answer on June 3, 2026 at 5:34 am

    You can simply use the DateTime.Parse() (msdn link) (or TryParse()) method to do this. Look at following example code:

    string[] times = new string[] 
    {
        "00:00 AM"
        , "01:00 AM"
        , "10:00 AM"
        , "12:00 AM"
        , "00:00 PM"
        , "01:00 PM"
        , "10:00 PM"
        , "12:00 PM"
    };
    
    foreach (var time in times)
    {
        DateTime date = DateTime.Parse(time);
        Console.WriteLine(date);
    }
    

    Gives output:

    03/05/2012 00:00:00
    03/05/2012 01:00:00
    03/05/2012 10:00:00
    03/05/2012 00:00:00
    03/05/2012 12:00:00
    03/05/2012 13:00:00
    03/05/2012 22:00:00
    03/05/2012 12:00:00
    

    In your case, just make a string that contains “hour”:”minutes” + “AM” or “PM”. In code that would be (if your input is invalid, the Parse() method throws an exception or else a very weird result)):

    public static DateTime GetDatetime(string ampm, string hour, string minute)
    {
        return DateTime.Parse(hour + ":" + minute + " " + ampm);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following method: public static DateTime SubQtrs( this DateTime dt, int qtrs
I have a page with this method in CreateTicket.aspx.cs: [WebMethod()] public static string Categories()
the ToSelectList method I have: public static IList<SelectListItem> ToSelectList<T>(this IEnumerable<T> itemsToMap, Func<T, string> textProperty,
I have a method that looks like this: public static String escape(String text) {
I have a very simple extension method that looks like this: public static string
I have this factory method in java: public static Properties getConfigFactory() throws ClassNotFoundException, IOException
I have this method: public void testJSNI2(){ String x = test; } I can
I have this method: public CampaignCreative GetCampaignCreativeById(int id) { using (var db = GetContext())
I have an extension method defined like so: public static TSource MaxBy<TSource, TResult>(this IEnumerable<TSource>
Is it possible to rewrite this extension method without the parameter? public static string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.