开发者

C# Date format and regional settings

开发者 https://www.devze.com 2023-03-08 06:12 出处:网络
UK settings: 1/7/2011 (July 1, 2011). When formatted in Excel, I use \"MMM-yy\", expected: Jul-11, but it gives me \"Jan-11\"

UK settings: 1/7/2011 (July 1, 2011). When formatted in Excel, I use "MMM-yy", expected: Jul-11, but it gives me "Jan-11"

My code:

newDataRange.SetValue("Value2", arr);  //arr = "01/07/2011 00:00:00.000"
newDataRange.SetValue("NumberFormat", "MMM开发者_开发知识库-yy");

        public static void SetValue(this Range range, string propertyName, object value)
        {
            var excelCulture = new CultureInfo(1033);
            var args = new object[1];
            args[0] = value;
            range.GetType().InvokeMember(propertyName, 
                BindingFlags.SetProperty | BindingFlags.Public | BindingFlags.Instance, null, range, args, excelCulture);
        }

I am confused. Anyone knows how to solve this thanks


It looks like the culture you are using has a date format of mm/dd/yyyy when you want dd/mm/yyyy. Thus, your conversion is using 1 from 1/7/2011 as the month instead of the 7.

Changing the culture to one which has a format of dd/mm/yyyy should fix your problem.

EDIT

As I suspected, LCID 1033 (0x0409) is en-US, not en-GB. en-GB is 2057 (0x0809). You should use the locale name to avoid this problem in the future, vis: new CultureInfo("en-GB");


It's clearly not UK settings - the date 1/7/2011 would show up as Jan 2011 in US date format

0

精彩评论

暂无评论...
验证码 换一张
取 消