[英]Formatting time from dd/mm/yyyy hh:mm:ss to yyyy.mm.dd hh:mm
I received the following task: turn "dd/mm/yyyy hh:mm:ss" time to "yyyy.mm.dd hh:mm" format. 我收到以下任务:将“ dd / mm / yyyy hh:mm:ss”时间转换为“ yyyy.mm.dd hh:mm”格式。
I was hoping that a simple Format(activecell.value, "yyyy.mm.dd hh:mm") would help, but. 我希望一个简单的Format(activecell.value,“ yyyy.mm.dd hh:mm”)会有所帮助,但是。 In some cases it works.
在某些情况下,它可以工作。 However, in some special cases, it doesn't.
但是,在某些特殊情况下,事实并非如此。 When the day is between 01-12 (interchangeable with months!), then it messes up the days with months.
当日期介于01-12之间(可以与月份互换!)时,它会使月份与月份混乱。 (Probably because of US style date format.)
(可能是由于美国风格的日期格式。)
For example, original text string: 04/06/2013 09:00:00 例如,原始文本字符串:04/06/2013 09:00:00
Expected: 2013.06.04 09:00 预计:2013.06.04 09:00
Result: 2013.04.06 09:00 (day and month have been swapped) 结果:2013.04.06 09:00(日期和月份已交换)
I tried to overcome it by formatting the input, I gave it a "dd/mm/yyyy hh:mm:ss" custom format. 我试图通过格式化输入来克服它,我给了它一个“ dd / mm / yyyy hh:mm:ss”自定义格式。 Didn't help, it still swaps the day and the month.
没有帮助,它仍然交换日期和月份。
So I have no ideas anymore, but regular expressions. 所以我不再有主意,而是正则表达式。 Date format is always the same so just moving the parts of the string would be okay.
日期格式始终相同,因此只需移动字符串的一部分就可以。 However, I don't know how that works and google searching brings up confusing results.
但是,我不知道它是如何工作的,谷歌搜索会带来令人困惑的结果。
Can anyone help me? 谁能帮我? Of course if there is a more elegant way to do it, that's also welcome.
当然,如果有更优雅的方法可以做到这一点,那也是值得欢迎的。
Try using this Custom Format: 尝试使用此自定义格式:
yyyy/mm/dd h:mm;@
To enter it, right click on the cell and Choose Format Cell: 要输入它,请右键单击该单元格,然后选择“设置单元格格式”:
Then under the Number
Tab Select Custom
from the listbox. 然后在“
Number
选项卡下,从列表框中选择“ Custom
”。 And enter the provided format in the Type:
textbox. 然后在“
Type:
文本框中输入提供的格式。
If that doesn't work. 如果那不起作用。 Are you *****Absolutely***** Positive that 04/06/2013 09:00:00 IS April 6, 2013 and NOT June 4, 2013??
您*****绝对*****肯定地说04/06/2013 09:00:00是2013年4月6日,而不是2013年6月4日吗?
If this still doesn't work and you have verified that dates are correct. 如果仍然无法使用,并且您已确认日期正确。
Sub ChangeDateFormat()
Application.ScreenUpdating = False
Dim CurrentCell As Range
Dim LastRow As Long
Dim RegEx As Object
Set RegEx = CreateObject("vbscript.regexp")
RegEx.Global = True
LastRow = Range("A" & Rows.Count).End(xlUp).Row 'Get The Last Row in Column Change A as Needed
For Each CurrentCell In Range("A1:A" & LastRow) ' Loop through all cells. Change Column as needed
If InStr(CurrentCell.Value, "/") <> 0 Then 'To make sure only convert non converted ones
RegEx.Pattern = "(\d{2})/(\d{2})/(\d{4}) (\d{2}):(\d{2}):(\d{2})" ' Seperate all parts of imported Data into groups
CurrentCell.Value = RegEx.Replace(CurrentCell.Value, "$3.$2.$1 $4:$5") ' Change order of groups and place into cell.
End If
Next
Application.ScreenUpdating = True
End Sub
*****NOTE: ***** This will only work if ALL date values are of dd/mm/yyyy hh:mm:ss
if they are not you will have to add some Error Handling and possibly slightly modify the code as it WILL cause problems. *****注意:*****仅当所有日期值均为
dd/mm/yyyy hh:mm:ss
时,此方法才有效,否则,您将不得不添加一些错误处理并可能略微修改代码因为它会导致问题。
This will also work on dates that are inside of other text. 这也适用于其他文本中的日期。 If the value of
A1
is 如果
A1
的值是
This is a test 04/06/2013 09:00:00 Lets see what happens
Then the new value will be 然后新值将是
This is a test 2013.06.04 09:00 Lets see what happens
its simple. 这很简单。 .
。 .
。 just follow the steps select the entire column or even required cells press right click and select format cell option, a tab window will open from number tab select date , from locale drop down menu select English (South Africa) from given options in type select your desired reverse format .
只需按照以下步骤选择整个列或什至是所需的单元格,请右键单击并选择格式单元格选项,将在数字选项卡选择日期中打开一个选项卡窗口,从区域设置下拉菜单中选择英语(南非),然后从给定的类型中选择您的所需的反向格式。 .
。 .
。 and you done :)
你完成了:)
如果月份和日期在错误的位置,则可以使用日期格式的"DATE(YEAR(A1),DAY(A1),MONTH(A1))"
和"DATE(MID(A1,7,4),LEFT(A1,2),MID(A1,4,2))"
如果为文本格式
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.