I'm unable get local time . im getting data like this from the api
sample data
2017-03-06T09:34:20.545Z
Desired Out put
3/6/2017, 3:04:20 PM
im getting value like this - 06/03/2017 04:34:20 AM
how to get the proper time in the format "3/6/2017, 3:04:20 PM".
i tried to localize the time but its giving incorrect date time.
data type
public string UpdatedTime { get; set; }
string updtime = bin.timestamp;//03/06/2017 12:51:33
binModel.UpdatedTime = Convert.ToDateTime(updtime).ToString("M/d/yyyy, h:mm:ss tt", CultureInfo.InvariantCulture);//expected time - 3/6/2017, 6:21:33 PM
Try this Code:
private void button1_Click(object sender, EventArgs e)
{
label1.Text = DateTime.Now.ToString("dd/MM/yyyy,hh:mm:ss tt"); // case sensitive
}
You have incorrect format here:
"dd/MM/yyyy HH:mm:ss tt"
HH
is used for 24 hours format, but you use AM/PM
so you need 12 hours format. Use hh
.
You sample data is:
2017-03-06T09:34:20.545Z
it means. year is 2017
, month is 03
, day is 06
and the same for time.
For desired output you need Month/Day/Year
. But you use this format:
"dd/MM/yyyy HH:mm:ss tt"
here you get Day/Month/Year
.
You should change your format to (also without zeros in front and with comma):
"M/d/yyyy, h:mm:ss tt"
I personally always reference this page when I forget how to format timestamps. If you must use strings, and not DateTime, you can do this.
obj.UpdatedTime = string.Format("{0: d/m/yyyy, HH:mm:ss tt", Convert.ToDateTime(data)});
The format of the string can be whatever your choosing, but the page linked has always been the most helpful for me. If you can use a DateTime property, just do this. You will always want to wrap this in a try/catch and verify you're getting a value you want.
obj.UpdatedTime = Convert.ToDateTime(data);
Cheers