我没有从 DateTime-Object 获得以下所需的输出:
2010-03-29 13:15:00
我的默认本地化是 de-DE,所以一个简单的 ToString 给了我:
29.03.2010 13:15:00
我尝试的是从 US-Culture 创建一个 DateTimeFormatInfo 对象并使用它的 DateTimePatterns 但没有成功:
2 3 | usDateFormat.DateSeparator ="-" ruleResult.Claim.Last_Updated_Date.ToString(usDateFormat.ShortDatePattern) |
输出:3.29.2010
什么是获得我想要的 DateTimeFormat 的最佳方式(可读、最快)(我有时需要短日期,有时需要日期和时间,就像我上面的例子一样)?
感谢您的期待,
蒂姆
编辑:为什么我的 DateTimeFormatInfo 方法不起作用,并且在使用 String.Format 或 ToString("yyyy-MM-dd HH:mm:ss") 解决方案而不是私有共享(静态) DateTimeFormatInfo 时会产生性能差异-目的?我至少调用了这个函数 30k 次,里面有很多日期要格式化。
我认为您正在寻找的是:
这里有一些例子,我想如果你看一下你就可以理解它是如何工作的:
2 3 4 5 6 7 8 9 10 11 12 13 | DateTime dateTime = new DateTime(2008, 3, 9, 16, 5, 7, 123); dateTime.ToString("y yy yyy yyyy"); //"8 08 008 2008" year dateTime.ToString("M MM MMM MMMM"); //"3 03 Mar March" month dateTime.ToString("d dd ddd dddd"); //"9 09 Sun Sunday" day dateTime.ToString("h hh H HH" ); //"4 04 16 16" hour 12/24 dateTime.ToString("m mm" ); //"5 05" minute dateTime.ToString("s ss" ); //"7 07" second dateTime.ToString("f ff fff ffff"); //"1 12 123 1230" sec.fraction dateTime.ToString("F FF FFF FFFF"); //"1 12 123 123" without zeroes dateTime.ToString("t tt" ); //"P PM" A.M. or P.M. dateTime.ToString("z zz zzz" ); //"-6 -06 -06:00" time zone |
要获得
有关详细信息,请参阅此 MSDN 文章
这个解决方案怎么样:
2 3 4 5 6 7 8 | var dateTimeFormat = CultureInfo.GetCultureInfo("en-US").DateTimeFormat; // Clone it to make it writeable dateTimeFormat = (DateTimeFormatInfo)dateTimeFormat.Clone(); // Change the DateSeparator dateTimeFormat.DateSeparator ="-"; // Output it as normal var output = DateTime.Now.ToString("g", dateTimeFormat); |
我认为大多数人不知道你可以通过调用
更新
正如蒂姆所说,你也可以这样称呼它:
2 3 4 5 6 | var dateTimeFormat = new CultureInfo("en-US").DateTimeFormat; // Change the DateSeparator dateTimeFormat.DateSeparator ="-"; // Output it as normal var output = DateTime.Now.ToString("g", dateTimeFormat); |
这行得通,因为我们创建了一个源自给定文化的新
所以说到底,两个版本都在做同样的事情,换句话说。如果两个版本之间存在性能差异,我无法判断,但我认为这可以忽略不计。
更新 2
在阅读了汤姆斯的最后一条评论后,他尝试了这样的事情:
2 3 4 5 6 | var dateTimeFormat = new CultureInfo("en-US").DateTimeFormat; // Change the DateSeparator dateTimeFormat.DateSeparator ="-"; // Output it as normal var output = DateTime.Now.ToString(dateTimeFormat.ShortDatePattern); |
这会导致错误的输出。这是因为在
是一样的
由于事实上,
2 3 4 5 6 | var dateTimeFormat = new CultureInfo("en-US").DateTimeFormat; // Change the DateSeparator dateTimeFormat.DateSeparator ="-"; // Output it as normal var output = DateTime.Now.ToString(dateTimeFormat.ShortDatePattern, dateTimeFormat); |
通过这些信息,您还应该能够对更快的情况做出一些假设:
2 | var output = DateTime.Now.ToString("M/d/yyyy", dateTimeFormat); |
由于它们完全相同,因此性能应该没有差异
您可以像这样在 DateTime 的 ToString 中使用格式化选项
长
短
2 3 | DateTime.Now.ToString("yyyy-MM-dd") |
你也可以试试
对于预定义的模式检查这个:
http://alexonasp.net/samples/stringformatting/
我想为Heroku构建一个Rails3应用程序。他们使用Postgres作为他们的数据库,所以我通过MacPorts安装了postgres9.0。现在我需要一个postgresgem并且共识是出于性能原因你想要pggem。但是我对我得到的错误感到非常困惑当我尝试在rvm下通过geminstall安装pg时。我已经非常明确地指定了所有postgres目录的位置可以找到但仍然无法完成安装:$envARCHFLAGS='-archx86_64'geminstallpg--\--with-pg-config=/opt/local/var/db/postgresql90/defaultdb/po
尝试通过RVM将RubyGems升级到版本1.8.10并出现此错误:$rvmrubygemslatestRemovingoldRubygemsfiles...Installingrubygems-1.8.10forruby-1.9.2-p180...ERROR:Errorrunning'GEM_PATH="/Users/foo/.rvm/gems/ruby-1.9.2-p180:/Users/foo/.rvm/gems/ruby-1.9.2-p180@global:/Users/foo/.rvm/gems/ruby-1.9.2-p180:/Users/foo/.rvm/gems/rub
我的最终目标是安装当前版本的RubyonRails。我在OSXMountainLion上运行。到目前为止,这是我的过程:已安装的RVM$\curl-Lhttps://get.rvm.io|bash-sstable检查已知(我假设已批准)安装$rvmlistknown我看到当前的稳定版本可用[ruby-]2.0.0[-p247]输入命令安装$rvminstall2.0.0-p247注意:我也试过这些安装命令$rvminstallruby-2.0.0-p247$rvminstallruby=2.0.0-p247我很快就无处可去了。结果:$rvminstall2.0.0-p247Search
由于fast-stemmer的问题,我很难安装我想要的任何rubygem。我把我得到的错误放在下面。Buildingnativeextensions.Thiscouldtakeawhile...ERROR:Errorinstallingfast-stemmer:ERROR:Failedtobuildgemnativeextension./System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/rubyextconf.rbcreatingMakefilemake"DESTDIR="cleanmake"DESTDIR=
当我尝试安装Ruby时遇到此错误。我试过查看this和this但无济于事➜~brewinstallrubyWarning:YouareusingOSX10.12.Wedonotprovidesupportforthispre-releaseversion.Youmayencounterbuildfailuresorotherbreakages.Pleasecreatepull-requestsinsteadoffilingissues.==>Installingdependenciesforruby:readline,libyaml,makedepend==>Installingrub
如何在ruby中调用C#dll? 最佳答案 我能想到几种可能性:为您的DLL编写(或找人编写)一个COM包装器,如果它还没有,则使用Ruby的WIN32OLE库来调用它;看看RubyCLR,其中一位作者是JohnLam,他继续在Microsoft从事IronRuby方面的工作。(估计不会再维护了,可能不支持.Net2.0以上的版本);正如其他地方已经提到的,看看使用IronRuby,如果这是您的技术选择。有一个主题是here.请注意,最后一篇文章实际上来自JohnLam(看起来像是2009年3月),他似乎很自在地断言RubyCL
我正在尝试在Ruby中复制Convert.ToBase64String()行为。这是我的C#代码:varsha1=newSHA1CryptoServiceProvider();varpasswordBytes=Encoding.UTF8.GetBytes("password");varpasswordHash=sha1.ComputeHash(passwordBytes);returnConvert.ToBase64String(passwordHash);//returns"W6ph5Mm5Pz8GgiULbPgzG37mj9g="当我在Ruby中尝试同样的事情时,我得到了相同sha
我正在尝试使用boilerpipe来自JRuby。我看过guide从JRuby调用Java,并成功地将它与另一个Java包一起使用,但无法弄清楚为什么同样的东西不能用于boilerpipe。我正在尝试基本上从JRuby中执行与此Java等效的操作:URLurl=newURL("http://www.example.com/some-location/index.html");Stringtext=ArticleExtractor.INSTANCE.getText(url);在JRuby中试过这个:require'java'url=java.net.URL.new("http://www
我意识到这可能是一个非常基本的问题,但我现在已经花了几天时间回过头来解决这个问题,但出于某种原因,Google就是没有帮助我。(我认为部分问题在于我是一个初学者,我不知道该问什么......)我也看过O'Reilly的RubyCookbook和RailsAPI,但我仍然停留在这个问题上.我找到了一些关于多态关系的信息,但它似乎不是我需要的(尽管如果我错了请告诉我)。我正在尝试调整MichaelHartl'stutorial创建一个包含用户、文章和评论的博客应用程序(不使用脚手架)。我希望评论既属于用户又属于文章。我的主要问题是:我不知道如何将当前文章的ID放入评论Controller。
首先回顾一下拉格朗日定理的内容:函数f(x)是在闭区间[a,b]上连续、开区间(a,b)上可导的函数,那么至少存在一个,使得:通过这个表达式我们可以知道,f(x)是函数的主体,a和b可以看作是主体函数f(x)中所取的两个值。那么可以有, 也就意味着我们可以用来替换 这种替换可以用在求某些多项式差的极限中。方法: 外层函数f(x)是一致的,并且h(x)和g(x)是等价无穷小。此时,利用拉格朗日定理,将原式替换为 ,再进行求解,往往会省去复合函数求极限的很多麻烦。使用要注意:1.要先找到主体函数f(x),即外层函数必须相同。2.f(x)找到后,复合部分是等价无穷小。3.要满足作差的形式。如果是加