如何整数日期转换为格式化日期字符串(即2012009至2009年2月1日) [英] How to convert a date of integers to a formated date string (i.e. 2012009 to 2/01/2009)

查看:381
本文介绍了如何整数日期转换为格式化日期字符串(即2012009至2009年2月1日)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何想法?
我不能拿出任何。



我有日期,我从CSV文件中加载的列表,将它们保存为所有的整数,或者更确切地说,整数的字符串(即2009年1月1日= 1012009)



如何开启1012009到2009年1月1日的任何想法?



谢谢!


解决方案

  INT日期= 1012009 ; 

VAR月=日期/ 1000000;
VAR天=(日期/ 10000)%100;
变种一年= DATE%10000;

VAR格式=新日期时间(年,月,日)的ToString();

这假定月 - 日 - 年;如果数字是日 - 月 - 年,我敢肯定,你就可以交换月份和日期的变量,以适应​​这一点。



如果你想定制日期格式,你可以这样做在描述的:




Any ideas? I can't come up with any.

I have a list of dates I'm loading in from a csv file and they are saved as all integers, or rather a string of integers (i.e. Jan 1, 2009 = 1012009)

Any ideas on how to turn 1012009 into 1/01/2009?

Thanks!

解决方案

int date = 1012009;

var month = date / 1000000;
var day = (date / 10000) % 100;
var year = date % 10000;

var formatted = new DateTime(year, month, day).ToString();

This assumes month-day-year; if the numbers are day-month-year, I’m sure you’ll be able to swap the month and day variables to accommodate that.

If you want to customise the date format, you can do so as described in:

这篇关于如何整数日期转换为格式化日期字符串(即2012009至2009年2月1日)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆