c# - Convert set of numbers to date -
i have string has format
20150622 yyyymmdd
but need convert following format
year-month-date 2015-06-22
is there way in c# need? don't see anyway treat strings arrays in c# there?
you can parse string datetime
yyyymmdd
format (there no dd
specifier) , generate it's string representation yyyy-mm-dd
format like;
string s = "20150622"; datetime dt = datetime.parseexact(s, "yyyymmdd", cultureinfo.invariantculture) console.writeline(dt.tostring("yyyy-mm-dd", cultureinfo.invariantculture));
output;
2015-06-22
but if string parts doesn't range in year, month , day of gregorian calendar, solution won't work. in such case, can use string.substring
method parts of string , format them -
delimiter like;
var s = "20150622"; var result = string.format("{0}-{1}-{2}", s.substring(0,4), s.substring(4,2), s.substring(6,2));
result;
2015-06-22
Comments
Post a Comment