C#时间格式问题解决,不受系统时间格式影响

陆陆续续写了几个程序,批量导入之后总会遇到这么几台报错的,然后一看就发现是车间设备系统时间格式不同。

所以,本着为现场排忧解难的态度(其实就是懒),格式化时间好了。

代码附上:

if (DateTimeFormatInfo.CurrentInfo != null)
            {
                var type = DateTimeFormatInfo.CurrentInfo.GetType();
                var field = type.GetField("generalLongTimePattern", BindingFlags.NonPublic | BindingFlags.Instance);
                if (field != null)
                    field.SetValue(DateTimeFormatInfo.CurrentInfo, "yyyy-MM-dd HH:mm:ss");
            }
为API生,为框架死,为debug奋斗一辈子;吃符号亏,上大小写的当,最后死在需求上。
原文地址:https://www.cnblogs.com/ChaunceyWan/p/11993278.html