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

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

问题描述

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



我有一个从csv文件加载的日期列表,它们被保存为所有整数,或者一串整数(即2009年1月1日= 1012009)



有关如何将1012009转换为1/01/2009的任何想法?



谢谢!

解决方案

  int date = 1012009 ; 

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

var formatted = new DateTime(年,月,日).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至2/01/2009)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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