首页 \ 问答 \ Swift:日期字符串只有月份和年份,我还可以生成日期吗?(Swift: Date string only has month and year, can I still produce a date?)

Swift:日期字符串只有月份和年份,我还可以生成日期吗?(Swift: Date string only has month and year, can I still produce a date?)

我正在将一些旧的Javascript代码移植到Swift应用程序,我的客户端的数据集有点奇怪,因为它们只记录了每个日期的月份和年份,所以安装日期字符串看起来像: 2001-05

在Javascript中我可以使用: var date = new Date("2001-05"); 它返回的内容: 2001-05-01T00:00:00.000Z将时间/日归零,但按预期保持年和月。

我目前在Swift中使用以下代码:

var date = "2001-05"

func stringToDate(_ date: String) -> Date? {
    let df = DateFormatter()
    df.locale = NSLocale(localeIdentifier: "en_US_POSIX") as Locale!
    df.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZZZZ"

    return df.date(from: date)
}

print(stringToDate(date))

这总是产生零输出。 是否有可能在Swift中重现日期格式的JS行为?


I'm porting some old Javascript code to a Swift app, my data-set from my client is a little strange in that they recorded just the month and year of each date, so an installation date string would look like: 2001-05.

In Javascript I can use: var date = new Date("2001-05"); to which it returns: 2001-05-01T00:00:00.000Z zeroing the time/day but maintaining the year and month as expected.

I currently use the below code in Swift:

var date = "2001-05"

func stringToDate(_ date: String) -> Date? {
    let df = DateFormatter()
    df.locale = NSLocale(localeIdentifier: "en_US_POSIX") as Locale!
    df.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZZZZ"

    return df.date(from: date)
}

print(stringToDate(date))

This always produces a nil output. Is it possible to reproduce the JS behaviour of date-formatting in Swift?


原文:
更新时间:2022-06-07 21:06

最满意答案

以下是来自TVITEM的 MSDN文档:

cchTextMax定义
pszText成员指向的缓冲区大小,以字符为单位。 如果此结构用于设置项属性,则忽略此成员。

所以这个成员只有在检索一个items属性时才有效,并且在设置时忽略 (就像在树视图控件中添加一个新项目一样)。 您的另一种解决方案是将所需文本复制到新指针,然后使用是一种选择。


Here is from MSDN documentation of TVITEM:

cchTextMax
Size of the buffer pointed to by the pszText member, in characters. If this structure is being used to set item attributes, this member is ignored.

So this member is only valid when you are retrieving an items attribute and ignored when you are setting it(as in adding a new item to a tree-view control). Your other solution of copying required text to new pointer, and then using is one option.

相关问答

更多
  • GetWindowTextLength()返回文本中TCHAR元素的数量,但GlobalAlloc()需要一个字节计数。 如果您正在编译Unicode, TCHAR是2个字节,而不是1个字节,但您没有考虑到这一点。 您也没有分配足够大的缓冲区来保存现有文本和正在追加的新文本。 你也在泄漏你分配的内存。 尝试这个: void AppendText( const HWND &hwnd, TCHAR *newText ) { // get edit control from dialog HWND ...
  • 您需要为此子项使用所有者绘制,以便它使用与列表视图的其余部分不同的字体属性。 You'll need to use owner-draw for this sub item so that it uses different font properties than the rest of the list view.
  • 您必须传递VBA RGB函数的值 。 ( Source )这是一个整数值, 不是一个整数值数组或字符串。 通过一些简单的位操作就可以轻松计算出RGB函数的值。 Red(r),Green(g)和Blue(b)分量的计算公式为: (r) | (g << 8) | (b << 16) 在哪里| 是按位或运算符, <<是左移运算符。 如果您希望在不使用按位操作的情况下工作,则还可以使用此计算: r + (g * 256) + (b * 65536) You have to pass the value of t ...
  • 正如MSDN所说 , DrawText函数使用设备上下文的选定字体,文本颜色和背景颜色来绘制文本。 这意味着您可以从设备上下文中获取当前的字体句柄,然后获得LOGFONT结构,该结构为您提供了所需字体的详细信息。 由于API使用的是当前选定的字体,因此需要使用新的字体句柄(通常是预先创建它,否则使用例如CreateFont或CreateFontIndirect API获取HFONT字体句柄)。 然后在调用DrawText之前,用您自己的SelectObject替换当前选定的字体,一旦完成,您就可以使用另一个 ...
  • 我假设您正在使用LVS_LIST模式,因为样式显示在您的代码示例中。 将项添加到列表控件后,可以使用LVM_SETCOLUMNWIDTH消息调整列大小。 您还可以使用ListView_SetColumnWidth宏。 例如: SendMessage(airlinelist, LVM_SETCOLUMNWIDTH, 0, 300); 这会将列设置为300像素宽。 如果您实际使用LVS_REPORT模式,则需要单独设置每列的宽度。 I'll assume you're using LVS_LIST mode ...
  • WM_CTLCOLORSTATIC是来自WM_COMMAND的单独消息。 除了对消息的检查位于对WM_COMMAND特定项目的检查之内,您希望处理的消息看起来是正确的。 尝试重新组织你的外部switch语句。 也许像下面这样: LRESULT CALLBACK DialogProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) { switch(message) { ...
  • 如果您在XP上运行,则可以通过LVGROUPMETRICS结构设置组标题颜色。 但是从你的屏幕截图来看,你显然是在Vista或更高版本。 不幸的是,在XP之后,这条消息什么也没做。 所以,抱歉,不,你无法改变颜色。 你甚至不能拥有它们。 If you are running on XP, you can set the group header color through the LVGROUPMETRICS structure. But from your screen shots, you are ob ...
  • LVS_EX_xxx样式是扩展列表视图样式 ,不是常规窗口样式。 您不能在CreateWindowEx调用中指定它们,您必须在创建窗口后使用LVM_SETEXTENDEDLISTVIEWSTYLE消息来设置它们。 例如, SendMessage(hwndListView, LVM_SETEXTENDEDLISTVIEWSTYLE, LVS_EX_FULLROWSELECT, LVS_EX_FULLROWSELECT); The LVS_EX_xxx styles are extended list ...
  • 以下是来自TVITEM的 MSDN文档: cchTextMax定义 pszText成员指向的缓冲区大小,以字符为单位。 如果此结构用于设置项属性,则忽略此成员。 所以这个成员只有在检索一个items属性时才有效,并且在设置它时忽略 (就像在树视图控件中添加一个新项目一样)。 您的另一种解决方案是将所需文本复制到新指针,然后使用是一种选择。 Here is from MSDN documentation of TVITEM: cchTextMax Size of the buffer pointed to b ...
  • 标准TreeView控件没有任何节点可见性的概念。 添加/删除节点是唯一的选择。 您必须维护节点显示的数据的单独链接列表缓存(无论如何您应该这样做,以便将UI逻辑与业务逻辑分开)。 否则,您需要编写自己的TreeView控件,或者找到满足您需求的第三方实现。 The standard TreeView control does not have any concept of node visibility. Adding/deleting nodes is the only option. You will ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)