草庐IT

datetime_index_test

全部标签

c# - 使用 DateTime.ToString ("tt"时,Windows 10 中的时间输出(AM/PM)发生了变化)

我最近升级到Windows10-现在我发现在使用“tt”格式说明符时日期的输出发生了一些意想不到的变化。下面是一些演示问题的代码:usingSystem.IO;usingSystem;usingSystem.Globalization;usingSystem.Threading.Tasks;classProgram{staticvoidMain(){varcultures=newstring[]{null,"en-NZ","en-US","en-AU","en-GB"};foreach(varcultureincultures){if(culture!=null){varc=Cultu

c# - 为什么 System.DateTime 结构具有布局类型 Auto?

结构System.DateTime及其堂兄System.DateTimeOffset将其结构布局类型设置为“自动”。这可以通过以下方式看到:typeof(DateTime).IsAutoLayout/*true*/或:typeof(DateTime).StructLayoutAttribute.Value/*Auto*/或者从声明的IL中可以看出:.classpublicautoansiserializablesealedbeforefieldinitSystem.DateTime¯¯¯¯通常情况下,用C#编写的结构(即不是枚举的.NET值类型)将具有“顺序”布局(除非已应用Struc

c# - 实验性特征 "indexed members"是什么?

论新RoslynPreviewsite它提到能够尝试潜在的语言特性,并列出了三个这样的特性。前两个我以前听说过(例如here),但我无法从代码示例中弄清楚“索引成员”是什么。谁能根据其他来源或代码示例解释这些是什么?(毫无值(value)$x不是C#5中的有效标识符。)更新-根据RoslynFeatureStatus页面,此功能已被撤销。 最佳答案 .$foobar只是["foobar"]的缩写形式 关于c#-实验性特征"indexedmembers"是什么?,我们在StackOverf

c# - 在 json 和 Web api 之间保持 C# datetime 本地时间?

当我在json对象中有数据时间时遇到问题,它将在C#dateTime中将其转换为UTC时区只是想问一下如何保持本地时间?我可以在web.config文件或geter或setter中设置时区属性,因为我必须反对有日期和时间吗?这是类示例?publicclassPatient{publiclongRecordId{get;set;}publicstringUsername{get;set;}publicDateTimeDate{get;set;}publicboolDeleted{get;set;}publicstringModifiedBy{get;set;}publicDateTime

c# - 使用 DateTime.Now.Ticks 生成唯一的数字 ID

我需要生成一个唯一的数字ID以附加到传入请求。此ID仅临时用于跟踪请求,一旦请求完成处理将被丢弃。此ID将仅在该应用程序的上下文中使用,但需要以高性能多线程方式分配。我正在考虑将DateTime.Now.Ticks用于此ID,但想知道如果同时处理同时请求,DateTime.Now.Ticks是否仍会生成冲突ID?如果有人可以建议一种在多线程环境中生成这些ID的更好方法(最好是不是像Tick那样的Int64的方法),请告诉我。像递增数字这样简单的东西就足够了,只要我不必在递增之前锁定数字即可。 最佳答案 您只需要使用一个静态变量,每次

c# - 遍历对象的属性并获取 DateTime 类型属性的值

我有一个对象列表(汽车)。对于列表中的每辆汽车,我需要遍历它并找到DateTime类型的任何属性。如果我找到DateTime的属性,我需要获取该值并进行时间转换。现在让我们将DateTime属性值打印到控制台。我无法理解我需要在prop.GetValue函数的第一个参数中输入什么。任何帮助,将不胜感激!foreach(varcarincarList){foreach(PropertyInfocarincar.GetType().GetProperties()){vartype=Nullable.GetUnderlyingType(prop.PropertyType)??prop.Pro

c# - 在 c# Parallel.ForEach 中的 List.Add() 上出现 "Index out of bounds"错误

这是代码Listsomething=newList();Parallel.ForEach(anotherList,r=>{..dosomeworksomething.Add(somedata);});Indexoutofbounds错误大约每百次运行1次。有没有办法防止由线程引起的冲突(我假设)? 最佳答案 为了防止出现此问题,您可以使用ConcurrentQueue而不是List或并行部分中的类似并发集合。并行任务完成后,您可以将其放入List中。.有关详细信息,请查看System.Collections.Concurrent命名

c# - 为什么 Dictionary[index] 会抛出 KeyNotFoundException 但 Hashtable[index] 不会?

知道为什么这种行为不同吗? 最佳答案 这是answer.TheprimaryreasonDictionarythrowsisthatthereisno"error"valuethatworksoveranyV.Hashtableisabletoreturnnullbecausethekeyisalwaysareferencetype. 关于c#-为什么Dictionary[index]会抛出KeyNotFoundException但Hashtable[index]不会?,我们在Stack

c# - 为什么没有 DateTime.AddWeeks(),如何获取 52 周前的 DateTime 对象?

System.DateTime对象具有AddYears()、AddMonths()、AddDays()、AddSeconds()等方法我注意到没有AddWeeks()。这是为什么?此外,我的要求是获取52周前的价格值。我知道这相当于1年,但具体是52周。我这样做会不会一样:yearOldPrice=_priceService.GetPriceForDate(price.Date.AddYears(-1));作为yearOldPrice=_priceService.GetPriceForDate(price.Date.AddDays(-7*52));我假设.AddDays(-7*52)与

c# - 在输入 json 中将值 {null} 转换为类型 'System.DateTime' 时出错

当我尝试在我的Json中输入以下DateTime参数时抛出此JsonSerializationException:"Errorconvertingvalue{null}totype'System.DateTime'ininputjson"我在这里给出了输入:stringinputJSONString="{....,\"StartDateFrom\":null,\"StartDateTo\":null,\"EndDateFrom\":null,\"EndDateTo\":null,\....}";反序列化使用:scT=(SearchCriteriaTask)JsonConvert.Des