Changing format of TODAY() in excel - excel

I'm using today to aquire todays date and then adding a static value to the end of it using the following:
=TODAY()&"T23:00:00"
Which Returns 43202T23:00:00
I really need it in the format 2018-04-12T23:00:00
Any help on this would be great!

There are a couple ways to accomplish this, depending on whether your goal is a formatted String (to display) or a numeric value (such as data type Date) for storing or using with calculations.
If you want a formatted date/time result (to display to the user)...
Use the TEXT worksheet function:
=TEXT(TODAY(),"yyyy-mm-dd")&"T23:00:00"
...the reason this works is because TODAY() returns a Date data type, which is basically just a number representing the date/time, (where 1 = midnight on January 1, 1900, 2 = midnight on January 2, 1900, 2.5 = noon on January 2, 1900,etc).
You can convert the date type to a String (text) with the TEXT function, in whatever format you like. The example above will display today's date as 2018-04-12.
If, for example, you wanted the date portion of the string displayed asApril 12, 2018 then you would instead use:
TEXT(TODAY(),"mmmm d, yyyy")
Note that the TEXT worksheet function (and VBA's Format function) always return Strings, ready to be concatenated with the rest of the String that you're trying to add ("T23:00:00").
If you want to use the result in calculations...
If you instead want the result to be in a Date type, then instead of concatenating a string (produced by the TEXT function) to a string (from "T23:00:00"), you could instead add a date to a date:
=TODAY()+TIME(23,0,0)
or
=TODAY()+TIMEVALUE("23:00")
..and then you can format it as you like to show or hide Y/M/D/H/M/S as necessary with Number Formats (shortcut: Ctrl+1).
More Information:
MSDN : TEXT Function (Excel)
MSDN : TIMEVALUE Function (Excel)
MSDN : TIME Function (Excel)

Related

How can I format a string into a date: time format?

For example:
2021-08-18T22:24:49-06:00
I want to print this to a more readable format like: 8/18/21 10:24pm
I have tried using the built in DateTime function but it returns an error. Can someone point me in the right direction? I have checked other answers but they all relate to using the aforementioned funciton.
Looking at how your data is formatted and it seems your data is formatted "yyyy-mm-ddThh:mm:ss";so, here is my attempt:
Formula in C1:
=--SUBSTITUTE(LEFT(A1,16),"T"," ")
Then I just formatted the resulting datetime-stamp with:
m/d/yy hh:mm AM/PM
So it remains a numeric value to do your calculations with if needed.
Give a try to below formula-
=TEXT(FILTERXML("<t><s>"&SUBSTITUTE(A1,"T","</s><s>")&"</s></t>","//s[1]")+FILTERXML("<t><s>"&SUBSTITUTE(FILTERXML("<t><s>"&SUBSTITUTE(A1,"T","</s><s>")&"</s></t>","//s[2]"),"-","</s><s>")&"</s></t>","//s[1]"),"M/dd/yyyy hh:mm AM/PM")
The date is in ISO 8601 format. This will parse out the different parts of the date string and convert to a date, assuming that your string is in A1:
=DATEVALUE(LEFT(A1,10))
+TIMEVALUE(MID(A1,12,8))
+TIMEVALUE(RIGHT(A1, 5))
*INT(MID(A1, 20, 1) & 1)
The first part grabs the date, the second part grabs the time, the third part captures the date offset, and the last part captures the sign. If you want to format that, you can do it with the cell formatting or wrap it in TEXT:
=TEXT(
DATEVALUE(LEFT(A1,10))
+TIMEVALUE(MID(A1,12,8))
+TIMEVALUE(RIGHT(A1, 5))
*INT(MID(A1, 20, 1) & 1),
"yyyy-mm-dd hh:mm:ss"
)
Note that if you need to support UTC, that is indicated by Z instead of a time offset, and you would need to modify the formula slightly. If your data always has the same time offset, you could just hardcode it, instead of parsing it out.

VBA Date + TimeValue returns no time

I have a date and time which I assemble into a date + time from strings in the form
date_string = "2020-12-30" 'yyyy-mm-dd
date_code = CDate(date_string)
time_string = "00:00:00" 'hh:mm:ss
time_code = TimeValue(time_string)
date_time = date_code + time_code
Commonly the return looks like 05.01.2019 11:00:00, which is what I expect.
The returned values also all check out as TRUE if I test with IsDate(date_time)
Whenever the time is 00:00:00 however, I only get the date returned with no time appended. I dont quite understand this, since TimeValue(time_string)returns 00:00:00.
So it must be an issue when combining date and time to a date + time string.
Can someone please enlighten me why midnight somehow does no exist in Excel VBA or where my error in creating the time code is?
EDIT:
I try to explain my situation a bit better:
I do this date date/time stuff in code and then but the result in an array in a loop. Only later on it is written to a cell in a table.
By the time is is written into a cell, even custom formatting the cell to "DD.MM.YYYY hh:mm" does not show the time as it is completely missing from the cell value.
Do I neet to apply a format at the point of date_code + time_code?
Sometimes the answer can be so simple. Thanks to Variatus and Paul I checked formatting out.
I applied a date_time = Format(date_code + time_code, "dd.mm.yyyy hh:mm") in my code. Using this, my code runs as expected and 00:00:00 appears as expected, even in the cell values of the Excel table.
When you enter an integer, like 43930, in a cell Excel will record the number as an integer, just as you entered it. You can then proceed to format the cell as #,##0.000 and thereby make the number display as 43930.000. Or you can format that very same number as custom dd mmm yyy hh:mm:ss and display it as 09 Apr 2020 00:00:00. The point is that Excel chose to record the number in its most efficient way, as an integer.
So, if you enter a DateValue + TimeValue which, together, amount to an integer Excel will record the integer correctly. The format in which that integer is displayed in your worksheet is a matter for cell formatting.

converting a twitter date string into a datetime field in excel

I have a number of excel strings in the format "Mon Nov 25 17:20:47 +0000 2019"
I found an earlier post that recommended using =DATE(RIGHT(O2,4),MONTH(DATEVALUE(1 & MID(O2,5,3))),MID(O2,9,2)) to create a usable date field. However, this drops the time which is an important piece of information.
How can I include the time with the date in order for excel to recognize and sort all the information included in the field?
Thank you in advance!
You can just use the same logic with the date formula, but use TIME instead of DATE and of course extract the correct time into the formula =TIME(MID(O2,12,2),MID(O2,15,2),MID(O2,18,2))
Edit:
to combine them both in one field, you will need to add them =DATE(RIGHT(O2,4),MONTH(DATEVALUE(1 & MID(O2,5,3))),MID(O2,9,2)) + TIME(MID(O2,12,2),MID(O2,15,2),MID(O2,18,2))
The rationale of this is because:
Date is expressed in whole numbers i.e. 1 = 01/01/1900, 2 = 02/01/1900, 3 = 03/01/1900... 43794 = 25/11/2019, etc.
Time is expressed as a fraction of the day i.e. 0.5 = 12 hrs/12PM, 0.66666 = 16 hrs/4PM, etc.
so lets say you have 1/1/2019 12.00 PM, the date part that gives 1/1/2019 will be 43466 and the time part will be 0.5. Adding them together will give you 43466.5, and when converted to a date time format it will show as 1/1/2019 12:00 PM.
You can use string functions to create an unambiguous date string, then turn it into a date/time value with a mathematical operation (adding the time value in a string form).
=(MID(A1,9,2)&"-"&MID(A1,5,3)&"-"&RIGHT(A1,4))+MID(A1,12,8)
You'll need to format the result as something appropriate: eg: dd-mmm-yyyy hh:mm

Formatting magento timestamp to readable excel format

I download invoice reports from my magento website. They hold a date format I am not able to change to a standard date format. This is what it looks like: 1 aug. 2016 15:24:08
What formula can I use to have excel change it to a format like 13-08-16 (Dutch Format) ( I don't need the time for the moment).
Assuming that your 3 letter month names end with a dot in magneto data, you can use this where original data is in cell A1
=TEXT(SUBSTITUTE(LEFT(A1,FIND(".",A1)+5),".",""),"dd-mm-yy")
Check if "1 aug. 2016 15:24:08" is:
a\ pure string (String Type data; "wroted" date)
or
b\ string representation of Date type data (formatted date)
.

Stata: how to change a string variable to a date?

I'm new to Stata, and I'm wondering how can I change a string variable which contains a date to a date format.
The data in the variable looks like this:
yyyy-mm-dd
Should I first remove the dashes so that Stata can recognize the format in order to later use gen var = date() ?
Thank you for your help.
The Stata date function is smart about removing separator characters. See help datetime_translation under the section "the date function"
If your dates are in v1 and in the form yyyy-mm-dd you can specify the commands:
generate v2 = date(v1, "YMD")
format %td v2
The YMD is called a mask, and it tells Stata the order in which the parts of the date are specified. The second line will assign the variable the Stata daily date format, which means that when you look at that variable in the data, it will be shown in human readable form. The date is stored, however, as the number of days since January 1, 1960.
The best way to experiment with the date function is to use the display command. The first line will display an integer representing the number of days since January 1, 1960. The second line will display the date in a human readable format.
display date("2013-08-14", "YMD")
display %td date("2013-08-14", "YMD")
you can look here to see how to convert to data in Stata or do like this
tostring datedx, replace
generate str4 dxyr1= substr(datedx,1,4)
generate str2 dxmo1 = substr(datedx,6,7)
generate str2 dxda1 = substr(datedx,9,10)
destring dx*, replace
gen datedx1 = mdy(dxmo1, dxda1, dxyr1)

Resources