json - Convert 2015-06-01T02:31:00+0000 to DateTime object c# -
i'm writting app consume webservice:
http://finance.yahoo.com/webservice/v1/symbols/allcurrencies/quote?format=json
as can see there, json object comes utc datetime field. want save information in simple datetime object following format "yyyy-mm-dd hh:mm:ss".
this code:
datetime dateparsed = datetime.now; datetime.tryparseexact((string)resource.selecttoken("resource").selecttoken("fields")["utctime"], "yyyy'-'mm'-'dd't'hh':'mm':'ssz", cultureinfo.invariantculture, system.globalization.datetimestyles.adjusttouniversal, out dateparsed);
i'm getting datetime object initialized in year 0001.
what i'm doing wrong?
thank in advance.
you have error in format-string. working sample:
using system; using system.globalization; public class program { public static void main() { datetime dateparsed = datetime.now; if ( datetime.tryparseexact( "2015-06-01t02:31:00+0000", "yyyy-mm-ddthh:mm:ss+0000", cultureinfo.invariantculture, system.globalization.datetimestyles.adjusttouniversal, out dateparsed ) ) { console.writeline(string.format("parsing done: {0:mm/dd/yyyy @ hh:mm}", dateparsed ) ); } else { console.writeline("no result"); } } }
note: +0000
hardcoded, when other values need detect them. if api returns +0 values, cut them off , work without z.
Comments
Post a Comment