转换日期例如2016年6月5日08:00:00到dd / mm / yyyy hh:mm:ss

时间:2016-07-01 14:37:00

标签: excel date excel-2010

获得主题标题中的输入。 试图找出如何将其转换为英国日期和时间以用于计算。

我在谷歌上看过一些方法,比如使用文字到列,但我认为这不是我想要的......

谢谢!

编辑:月份总是缩写格式 编辑2:我应该提到我在英国,它似乎没有自动转换美国日期 编辑3:数据:

Jun 05 2016 08:00:00 to dd / mm / yyyy hh:mm:ss

3 个答案:

答案 0 :(得分:2)

如果您不想在VBA中编写宏,如果格式符合您的指定,则单元格公式将起作用:

我假设数据在单元格B3中

=MID(B3,5,2) & "/" & IF(LEFT(B3,3)="Jan","01",IF(LEFT(B3,3)="Feb","02",IF(LEFT(B3,3)="Mar","03",IF(LEFT(B3,3)="Apr","04",IF(LEFT(B3,3)="May","05",IF(LEFT(B3,3)="Jun","06",IF(LEFT(B3,3)="Jul","07",IF(LEFT(B3,3)="Aug","08",IF(LEFT(B3,3)="Sep","09",IF(LEFT(B3,3)="Oct","10",IF(LEFT(B3,3)="Nov","11","12"))))))))))) & "/" & RIGHT(B3,13)

答案 1 :(得分:2)

假设您的源日期是一个字符串并且它在单元格D10中,您需要做的第一件事就是将其转换为Excel日期时间序列。在Excel Date Time序列中,有几点需要注意。

  • 数字的整数部分表示自1900年1月1日以来的天数,我认为1904年是mac
  • 数字的小数部分表示一天中的分数的时间。 0.5代表中午。 VBA的有效Excel时间为00:00至23:59:59。 24:00不是有效时间,但它可以使用一些excel公式

因此,为了将您的字符串转换为Excel日期序列,我们需要删除组件并将它们转储到DATE()函数中。日期函数由三个参数组成:

DATE(year,month,day)

非常直接,但这些值必须是数字。为什么我们不开始将您的信息从最大单位转移到最小单位。

谢天谢地,你的字符串长度一致。您的单位数字前导0,因此它们将占用与双位数相同的空格。所以这种方法可以使用到9999年,但我不认为我们现在太担心了。

为了拉动这一年,我们会看看你的字符串中的位置以及它的长度。因此,通过简单计数,我们知道它从第8个字符位置开始,其长度为4个字符。我们将此信息与MID()函数

一起使用
=MID(D10,8,4)

为了拉月,它变得有点复杂,因为我们需要将它从缩写转换为数字。有几种方法可以做到这一点。你可以去做一个很长的IF声明,最终会重复11个月的拉动。相反,我决定使用MATCH()函数并在其中构建一个月份缩写数组。 MATCH()函数将返回您在提供的搜索列表中搜索的数字/位置。因此,只要我们按时间顺序输入月份,它们的位置就会对应于它们的数值。因此,我们的公式将如下所示:

MATCH(LEFT(D10,3),{"JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC"},0)

LEFT()函数用于从字符串中提取月份缩写。最后的0告诉匹配寻找完全匹配。需要注意的是,此匹配方法不区分大小写。

现在,为了得到这一天,我们采用与拉动年度相同的原则,我们结束了:

=MID(D10,5,2)

我们现在可以将年月和日的每个公式替换为DATE()函数,我们将获得excel日期序列的日期部分。该公式应如下所示:

=DATE(MID(D10,8,4),MATCH(LEFT(D10,3),{"JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC"},0),MID(D10,5,2))

现在你需要确定你的时间部分或计算出小数部分。为了做到这一点,我首先建议尝试TIMEVALUE()函数。由于时间格式在格式上往往比日期更加标准,因此它对您有用的概率要高得多。为了使用TIMEVALUE(),需要从字符串中删除时间部分。这可以通过RIGHT()函数轻松完成,如下所示:

=RIGHT(D10,8)

这将为您提供时间部分,然后可以将其替换为TIMEVALUE()函数,如下所示:

=TIMEVALUE(RIGHT(D10,8))

如果TIMEVALUE()函数不适合您,则需要去掉小时分和秒并将其结果转储到TIME()函数中。这样做就像你拉动DATE()函数的年份和日期一样。只需更新你的角色数量。 TIME()使用如下三个参数:

TIME(HOUR,MINUTES,SECONDS)

既然您已经找到了日期部分和时间部分,那么您需要将它们添加到一起以将所有信息整合到一个单元格中。结果公式如下:

=DATE(MID(D10,8,4),MATCH(LEFT(D10,3),{"JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC"},0),MID(D10,5,2))+TIMEVALUE(RIGHT(D10,8))

无论您最终放置该公式,请记住将单元格上的格式更改为自定义日期。输入单元格自定义格式,如下图所示。

Custom Format

如果您有要在列中转换的日期时间列表,只需将您的公式和格式化的单元格尽可能地复制到您需要的位置。

概念证明

Proof of Concept

使用的公式

有关上述公式中使用的函数的更多信息,请点击以下链接:

{" JAN"" FEB"" MAR"" APR"" MAY",& #34; JUN"" JUL"" AUG"" SEP"" OCT"" NOV&# 34;," DEC"}

{}用于构建自定义列表或静态数组。

答案 2 :(得分:1)

DATEVALUE function期望在日期和年份之间使用逗号; REPLACE function可以添加该内容。TIMEVALUE function应该能够无需修改即可读取该时间。

W/IInputConnectionWrapper: endBatchEdit on inactive InputConnection
W/Resources: Converting to string: TypedValue{t=0x10/d=0x3 a=-1}
W/Resources: Converting to string: TypedValue{t=0x10/d=0x3 a=-1}
W/FragmentManager: moveToState: Fragment state for Tab2Fragment{320be629 #0 id=0x7f0e0078 android:switcher:2131624056:1} not updated inline; expected state 3 found 2
W/IInputConnectionWrapper: beginBatchEdit on inactive InputConnection
W/IInputConnectionWrapper: endBatchEdit on inactive InputConnection
W/IInputConnectionWrapper: beginBatchEdit on inactive InputConnection
W/IInputConnectionWrapper: endBatchEdit on inactive InputConnection
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
     Process: com.example.andrea.myapplication, PID: 4600
     android.view.InflateException: Binary XML file line #9: Error inflating class fragment
         at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:763)
         at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
         at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
         at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
         at com.example.andrea.myapplication.Impostazioni.onCreateView(Impostazioni.java:35)
         at android.support.v4.app.Fragment.performCreateView(Fragment.java:1974)
         at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
         at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1252)
         at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:742)
         at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1617)
         at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:517)
         at android.os.Handler.handleCallback(Handler.java:739)
         at android.os.Handler.dispatchMessage(Handler.java:95)
         at android.os.Looper.loop(Looper.java:135)
         at android.app.ActivityThread.main(ActivityThread.java:5289)
         at java.lang.reflect.Method.invoke(Native Method)
         at java.lang.reflect.Method.invoke(Method.java:372)
         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
      Caused by: java.lang.IllegalArgumentException: Binary XML file line #9: Duplicate id 0x7f0e0061, tag null, or parent id 0xffffffff with another fragment for com.example.andrea.myapplication.MyPreferenceFragment$PrefsFragment
         at android.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2120)
         at android.app.Activity.onCreateView(Activity.java:5367)
         at android.support.v4.app.BaseFragmentActivityHoneycomb.onCreateView(BaseFragmentActivityHoneycomb.java:34)
         at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:80)
         at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:733)
         at android.view.LayoutInflater.rInflate(LayoutInflater.java:806) 
         at android.view.LayoutInflater.inflate(LayoutInflater.java:504) 
         at android.view.LayoutInflater.inflate(LayoutInflater.java:414) 
         at com.example.andrea.myapplication.Impostazioni.onCreateView(Impostazioni.java:35) 
         at android.support.v4.app.Fragment.performCreateView(Fragment.java:1974) 
         at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067) 
         at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1252) 
         at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:742) 
         at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1617) 
         at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:517) 
         at android.os.Handler.handleCallback(Handler.java:739) 
         at android.os.Handler.dispatchMessage(Handler.java:95) 
         at android.os.Looper.loop(Looper.java:135) 
         at android.app.ActivityThread.main(ActivityThread.java:5289) 
         at java.lang.reflect.Method.invoke(Native Method) 
         at java.lang.reflect.Method.invoke(Method.java:372) 
         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) 
         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 
I/Process: Sending signal. PID: 4600 SIG: 9

请注意,A1中的原稿是左对齐的;这表示文本值。 B1中转换的日期/时间是右对齐的;这表示真实的日期/时间值。

B列格式为=DATEVALUE(REPLACE(LEFT(A1, 11), 7, 0, ","))+TIMEVALUE(RIGHT(A1, 8)) 。如dd/mm/yyyy hh:mm:ss,它会显示为General

datevalue_timevalue2

相关问题