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?
原文:
最满意答案
以下是来自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替换当前选定的字体,一旦完成,您就可以使用另一个 ...
-
win32列表视图缩写文本(win32 List View Abbreviating Text)[2023-06-18]
我假设您正在使用LVS_LIST模式,因为样式显示在您的代码示例中。 将项添加到列表控件后,可以使用LVM_SETCOLUMNWIDTH消息调整列大小。 您还可以使用ListView_SetColumnWidth宏。 例如: SendMessage(airlinelist, LVM_SETCOLUMNWIDTH, 0, 300); 这会将列设置为300像素宽。 如果您实际使用LVS_REPORT模式,则需要单独设置每列的宽度。 I'll assume you're using LVS_LIST mode ... -
设置静态文本颜色Win32(Set static text color Win32)[2022-03-26]
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 ...
-
如何使用Win32 api将树视图项设置为可见/不可见(How to set Tree View item as visible/invisible using Win32 api)[2022-06-18]
标准TreeView控件没有任何节点可见性的概念。 添加/删除节点是唯一的选择。 您必须维护节点显示的数据的单独链接列表缓存(无论如何您应该这样做,以便将UI逻辑与业务逻辑分开)。 否则,您需要编写自己的TreeView控件,或者找到满足您需求的第三方实现。 The standard TreeView control does not have any concept of node visibility. Adding/deleting nodes is the only option. You will ...