How to set AM/PM in a textbox using JavaScript?

You can automatically format time and append AM/PM in a textbox using JavaScript.

1. Set Current Time with AM/PM in TextBox

2. Convert 24-Hour Time to AM/PM (User Input)

function convertToAmPm(time) {
    let [hours, minutes] = time.split(":");
    hours = parseInt(hours, 10);

    let ampm = hours >= 12 ? "PM" : "AM";
    hours = hours % 12 || 12;

    return hours + ":" + minutes + " " + ampm;
}

// Example
console.log(convertToAmPm("18:30")); // Output: 6:30 PM

3. Auto-Append AM/PM While Typing (Optional)

4. Common Use Cases

  • ✔ Time entry forms
  • ✔ Attendance systems
  • ✔ Scheduling applications
  • ✔ Booking systems


How do you consume Web Services in C# using .NET?

1️⃣ Consume REST Web Service in C# 

✅ Using HttpClient (Best Practice)

using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;

public class ApiService
{
    private static readonly HttpClient client = new HttpClient();

    public async Task<string> GetDataAsync()
    {
        client.BaseAddress = new Uri("https://api.example.com/");
        client.DefaultRequestHeaders.Accept.Clear();
        client.DefaultRequestHeaders.Accept.Add(
            new MediaTypeWithQualityHeaderValue("application/json"));

        HttpResponseMessage response = await client.GetAsync("users");

        if (response.IsSuccessStatusCode)
        {
            return await response.Content.ReadAsStringAsync();
        }
        return null;
    }
}

✔ Supports async/await

✔ High performance

✔ Used in .NET Core / .NET 6+

✅ Deserialize JSON Response

using Newtonsoft.Json;

public class User
{
    public int Id { get; set; }
    public string Name { get; set; }
}

var json = await api.GetDataAsync();
var users = JsonConvert.DeserializeObject<List<User>>(json);
// (You can also use System.Text.Json in .NET 6+)

2️⃣ POST Data to REST API

public async Task PostDataAsync()
{
    var data = new { Name = "John", Age = 30 };
    var content = new StringContent(
        JsonConvert.SerializeObject(data),
        Encoding.UTF8,
        "application/json");

    await client.PostAsync("users", content);
}

3️⃣ Consume SOAP Web Service in C# (Legacy Systems)

✅ Step-by-Step (Visual Studio)

  • Right-click project → Add Service Reference
  • Enter WSDL URL
  • Click Go → OK

Example Usage

ServiceReference1.MySoapClient client =
    new ServiceReference1.MySoapClient();

var result = client.GetEmployeeDetails(101);

✔ Automatically generates proxy classes

✔ Best for legacy enterprise apps

4️⃣ Consume Web Service Using WebClient (Old – Not Recommended)

using (WebClient wc = new WebClient())
{
    string data = wc.DownloadString("https://api.example.com/data");
}

⚠ Deprecated

⚠ Avoid in new projects

5️⃣ Handle Authentication (Bearer Token)

client.DefaultRequestHeaders.Authorization =
    new AuthenticationHeaderValue("Bearer", "your_token_here");

6️⃣ Error Handling Best Practice

try
{
    var response = await client.GetAsync("users");
    response.EnsureSuccessStatusCode();
}
catch (HttpRequestException ex)
{
    Console.WriteLine(ex.Message);
}

🧠 Interview Comparison

TypeTechnologyUsage
RESTHttpClientModern apps
SOAPService ReferenceEnterprise legacy
Old RESTWebClientAvoid
JSONSystem.Text.JsonFast & lightweight

🚀 Best Practices (Important)

  • ✔ Reuse HttpClient
  • ✔ Use async/await
  • ✔ Handle timeouts
  • ✔ Secure API keys
  • ✔ Validate response