Solved

Excel convert date to int macro

Posted on 2014-01-29
9
604 Views
Last Modified: 2014-01-29
Hi,

I have a excel file with date column in many different format.

My database accept it as int(10), so example - 1387270706

Can you advice how i can convert my date(different format of date) to the above format.?

Thanks,
date.csv
0
Comment
Question by:magento
  • 5
  • 4
9 Comments
 
LVL 35

Expert Comment

by:mvidas
ID: 39818732
Hi magento,

I assume you're using unix date format, which I think is the number of seconds since Jan 1, 1970. If so, you can get it using the DateDiff function ("date difference") and specify seconds. You can easily incorporate it into a macro, or if you want to have a worksheet function just use:
Function ExcelDateToInt10(dateIn As Variant)
 ExcelDateToInt10 = DateDiff("s", #1/1/1970#, dateIn)
End Function

Open in new window

Matt

EDIT: Changed dateIn to Variant from double, to account for some formats
0
 
LVL 5

Author Comment

by:magento
ID: 39818747
how to run this as a macro please?
0
 
LVL 35

Accepted Solution

by:
mvidas earned 500 total points
ID: 39818807
If you want the results in your file you have two options.

1) Open your file, right-click the worksheet tab at the bottom and go to View Code, in the VBA editor go to Insert then Module, and paste in the above code. Close the VBA editor, and use it like a normal function in your workbook: =ExcelDateToInt10(B2)

2) If you want to run a macro to replace the cell contents with the converted number, run the following (changing the ranges as necessary):
Sub AnExample()
 Dim CLL As Range, dateOut As Variant
 For Each CLL In Range("B2:B1861").Cells
  dateOut = "ERROR: " & CLL.Text
  On Error Resume Next
  dateOut = DateDiff("s", #1/1/1970#, CLL.Value)
  On Error GoTo 0
  If dateOut < 0 Then dateOut = "ERROR: " & CLL.Text
  CLL.Value = dateOut
 Next
 Range("B2:B1861").NumberFormat = "General"
End Sub

Open in new window

Note that in some cases it will display "ERROR: " and the cell's contents instead of the current number. It will do this for invalid dates, like "ERROR: Jan18" or "ERROR: Sep 11, 200"
0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 5

Author Comment

by:magento
ID: 39819045
I have a date like below, but it didnt get converted after the macro

11-Oct-07

Please advice
0
 
LVL 35

Expert Comment

by:mvidas
ID: 39819089
There is no reason that wouldn't have been converted, at least as given there. Is it possible the O in "Oct" is really a zero, or the zero in "07" is a capital "O" or something?

If not, can you show me exactly what the cell says?
If you did it my #1 way above (and got a #VALUE! response from the formula), can you use
="ERROR: "&B2
and substitute the cell address for B2, and paste me the result?

If you did my #2 way above, can you paste for me the result there? I'm guessing there must be a strange character or something in the cell which is messing it up.
0
 
LVL 5

Author Comment

by:magento
ID: 39819113
Hi ,

I tried your macro , and all cells in column B has been changed correctly and only the few as provided above shows  the same value.

TimeStamp_original      TimeStamp_converted
19-Jan-14      1390167180
20-Jan-14      1390208340
Jan 26       Jan 26 
27-Jan-14      27-Jan-14
27-Jan-14      27-Jan-14


Thanks
0
 
LVL 5

Author Comment

by:magento
ID: 39819118
The file is attached for ur reference
date-ee.csv
0
 
LVL 35

Expert Comment

by:mvidas
ID: 39819144
When I ran the code on your attachment, the only errors I got were:
ERROR:  Jul 21, 201
ERROR:  May 12, 200
ERROR:  Sep 17, 201
ERROR: Dec 30, 201
ERROR: Jul 15, 201
ERROR: Jul 15, 201
ERROR: May 18, 201
ERROR: ug 4, 2007
ERROR: ul 8, 2007

My guess as to why some cells didn't get converted could be because your input range wasn't updated? My original code predefined the range to work until row 1861, is your other file longer?

Even cells like "Jan 26" should convert without issue, Excel should interpret that as the current year.
0
 
LVL 5

Author Comment

by:magento
ID: 39819944
My stupid, i didnt changed the values. Thank you.

I havent tried ur 1st solution .(I know it should work)

The 2nd solution works like charm.

Thanks again.
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Microsoft Office Picture Manager was included in Office 2003, 2007, and 2010, but not in Office 2013. Users had hopes that it would be in Office 2016/Office 365, but it is not. Fortunately, the same zero-cost technique that works to install it with …
Recently Microsoft released a brand new function called CONCAT. It's supposed to replace its predecessor CONCATENATE. But how does it work? And what's new? In this article, we take a closer look at all of this - we even included an exercise file for…
Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question