如何为整个C sharp windows项目制作相同的日期格式。 [英] How to make same date format for whole C sharp windows project.

查看:66
本文介绍了如何为整个C sharp windows项目制作相同的日期格式。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试开发一个软件项目,我希望我的整个项目使用相同的日期格式,无论系统日期格式是什么,即使我使用DateTime.Now。帮帮我,请给出建议或解决方案。



我尝试过:



 String.Format({0:dd-MM-yyyy},dt); 



i使用这个,但我不想每次使用日期时写这段代码

解决方案

< blockquote>你真的没有选择:DateTime值根本就没有格式,它存储为自过去一个固定点以来的一些滴答。当您决定将其转换为字符串进行显示时,它只会获得一种格式。如果您没有指定格式,它会使用当前的PC文化。



您可以为您的应用程序设置默认文化:

 CultureInfo.DefaultThreadCurrentCulture = new CultureInfo(en-GB); 

这会强制默认格式与该文化相匹配,但是......这也会影响默认文化用于解析用户输入日期所以它可能是一个坏主意。


当你要求.net将日期转换为字符串而不给它格式时它使用ShortDatePattern,所以你要必须通过更改操作系统设置(我猜你不能做)或在代码中进行更改;



asp.net - 如何更改当前文化的数据格式,以便它适用于整个Web应用程序 - Stack Overflow [ ^ ]



这是相当糟糕的做法,但你最好使用从配置设置中获得的显式格式\ helper function \扩展方法,即使它确实意味着更多的代码。



公共静态类MyExtensions 
{
public static string ToCustomDate(this DateTime dt)
{
return dt.ToString(dd-MM-yyyy);
}
}





 DateTime.Now.ToCustomDate()


您好b $ b

登录时,您可以拨打以下电话,它会帮助您。



  public   string  NewDateFormat( string  InputDate, string 格式)
{
var CurrentDate = DateTime.Parse(InputDate);
return CurrentDate.ToString(Format);
}


I am trying to develop a software project, i want my whole project to use the same date format no matter what system date format is even if i use DateTime.Now. Help me please give suggestion or solution anything.

What I have tried:

String.Format("{0:dd-MM-yyyy}", dt);


i use this but i don't want to write this code everytime i use date

解决方案

You don't really have a choice: a DateTime value doesn't have a format at all, it's stored as a number of ticks since a fixed point in the past. It only gets a format when you decide to convert it to a string for display. And if you don't specify a format it uses the current PC culture.

You could set the default culture for your application:

CultureInfo.DefaultThreadCurrentCulture = new CultureInfo("en-GB");

And that would force the default format to match that culture, but ... that would also affect the default culture used for parsing user input dates so it's probably a bad idea.


When you ask .net to convert a date to a string without giving it a format it uses the ShortDatePattern, so you'd have to change this either by changing the OS settings (which I'm guessing you can't do) or by doing it in code;

asp.net - How to change the data format for the current culture so that it applies to the entire web application - Stack Overflow[^]

This is fairly bad practice though, you're better using explicit formats that you get from a config setting\helper function\extension method\whatever even if it does mean more code.

public static class MyExtensions
{
    public static string ToCustomDate(this DateTime dt)
    {
        return dt.ToString("dd-MM-yyyy");
    }
}



DateTime.Now.ToCustomDate()


Hi
During login you call below function it will help you.

public string NewDateFormat(string InputDate, string Format)
{
    var CurrentDate = DateTime.Parse(InputDate);
    return CurrentDate.ToString(Format);
}


这篇关于如何为整个C sharp windows项目制作相同的日期格式。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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