IIS和控制台应用程序之间的DateTime.ToString(“d”,cultureInfo)输出不同

时间:2016-07-22 09:29:29

标签: c# date datetime tostring culture

我正在使用DateTime.Now.ToString("d", new CultureInfo("nl-NL"))

在本地IIS Web应用程序中,输出正确:

22-7-2016

在控制台应用程序中,框架选择国际/文化不变的日期符号:

2016-07-22

这是在同一台机器上,代码完全相同。我已经放置了断点,检查了所有输入,甚至尝试在此时立即在窗口中运行命令。在任何情况下,控制台应用程序都会出错。

有人可以解释导致差异的原因吗?

2 个答案:

答案 0 :(得分:5)

IIS和您的控制台应用程序在不同的用户帐户下运行。如果您已自定义日期格式(例如,使用“区域”控制面板),module Spree module Admin class LookBookImagesController < ResourceController before_action :set_look_book before_action :set_look_book_image, only: %i(edit update destroy) respond_to :html def index @look_book_images = @look_book.look_book_images.all end def new @look_book_image = @look_book.look_book_images.build @look_book_image.look_book_image_products.build end def edit; end def create @look_book_image = @look_book.look_book_images.build(look_book_image_params) @look_book_image.attributes = look_book_image_params if @look_book_image.save flash[:success] = "Image has been created." redirect_to admin_look_book_look_book_images_path else invoke_callbacks(:create, :fails) respond_with(@look_book_image) do |format| format.html { render action: :new } format.js { render layout: false } end end end def destroy @look_book_image.destroy end def update_positions ActiveRecord::Base.transaction do params[:positions].each do |id, index| model_class.find(id).set_list_position(index) end end respond_to do |format| format.js { render text: 'Ok' } end end private def set_look_book_image @look_book_image = @look_book.look_book_images.find(params[:id]) rescue ActiveRecord::RecordNotFound flash[:alert] = "The image you were looking for could not be found." end def set_look_book @look_book = LookBook.find(params[:look_book_id]) end def look_book_image_params params.require(:look_book_image).permit(:alt_text, :attachment, :look_book_id, look_book_image_products_attributes: [:id, :look_book_image_id, :spree_product_id, :_destroy] ) end end end end 格式化程序将仅在您的帐户中使用该自定义。

您可以创建一个忽略用户设置的 <script> $(document).ready(function(){ $("#clickShow").on('click', function(){ $("#showDescription").show(); $("#showDescription").find("a").focus(); }); }); </script> <div> <a href="#" id="clickShow">View Hidden Text</a> </div> <div id="showDescription" style="display:none;">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap <a href="#">Link Text</a> into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</div>

CultureInfo

您重置了帐户的区域设置:运行&gt; CultureInfo&gt;在下拉列表中选择NL语言环境&gt;其他设置&gt;复位。

答案 1 :(得分:-1)

我在VS 2012中使用.NET 4.5在控制台和Web应用程序中获得了相同的结果。

你可以尝试一下吗?

yourDateTime.ToString("dd-MM-yyyy", new System.Globalization.CultureInfo("nl-NL"));
相关问题