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:
You have incorrect format here:
HH
is used for 24 hours format, but you useAM/PM
so you need 12 hours format. Usehh
.You sample data is:
it means. year is
2017
, month is03
, day is06
and the same for time.For desired output you need
Month/Day/Year
. But you use this format:here you get
Day/Month/Year
.You should change your format to (also without zeros in front and with comma):
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.
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.
Cheers