Convert date & time string to date & time format in Bash - string

I have a log file which contains entries in this format:
Feb 15 14:28:37 [8085][8095] ssnotify.cpp:442:Send().....
How can I convert the date and time string to a date/time format so I can use it with other date/time commands within a bash script?

You can use the "date" command for this task.
Example:
user#host:so$ date --date="Feb 15 14:28:37"
jue feb 15 14:28:37 CET 2018
user#host:so$ date --date="Feb 15 14:28:37" +%s
1518701317
If you do: "man date" you will see all the options about how to format the date as you wish.

Related

Freemarker ISO string to datetime with timezone

I need to display the below "String" in the desired format
String str = 1979-01-24T00:00:00.000-08:00
Desired format: Jan 24, 1979 00:00:00 AM PST
Note: The tz in the str could be any tz not limited to PST.
Tried the below but none worked:
str?datetime.iso - Output is Jan 24, 1979 2:00:00 AM CST - This displays the date time in the format I need but the time is being converted from PST to CST.
str?string("MMM dd, yyyy hh:mm:ss a zzz") - Error: Expected a method, but this has evaluated to a string
str?datetime?string("MMM dd, yyyy hh:mm:ss a zzz") - Error: Unparseable date: "1979-01-24T00:00:00.000-08:00"
<#setting datetime_format="iso"> str?datetime - 1979-01-24T02:00:00-06:00 - The timezone is changed.
The problem here is that FreeMarker parses date/time values to java.util.Date (and its subclasses), which don't store a time zone anymore, as it always stores the value in UTC. So that information is lost after parsing. As of 2.3.30, the only solution I see to do this in Java (with Java 8 ZonedDateTime).
The timezone can be configured by the following setting, as refer to their documentation https://freemarker.apache.org/docs/ref_directive_setting.html
<#setting time_zone ="PST">
<#assign str = "1979-01-24T00:00:00.000-08:00">
${str?datetime.iso}

Possible to specify UTC+3 to linux date when creating a timestamp string?

I am trying to create a timestamp string:
TS=$(date -d "today" +"%Y_%d_%m_%H%M%S")
echo "TS = $TS"
But I need it to be in UTC+3. The man pages on date does not show that as an option and I don't want to modify the OS locale.
I have tried:
$ date -d "today" +"%Y_%d_%m_%H%M%S +0300"
2020_16_04_090342 +0300
$ date -d "today" +"%Y_%d_%m_%H%M%S +0400"
2020_16_04_090347 +0400
So seems it has no effect. Also the string should NOT contain the offset, should just be:
2020_16_04_120347
Any suggestions?
man date
DATE STRING
The --date=STRING is a mostly free format human readable date string such as "Sun, 29 Feb 2004 16:21:42 -0800" or "2004-02-29 16:21:42" or even "next Thursday". A date string may contain items indicating calendar date, time of day, time zone, day of week, relative time, relative date, and numbers. An empty string indicates the beginning of the day. The date string format is more complex than is easily documented here but is fully described in the info documentation.
date -d "today" +"%Y_%d_%m_%H%M%S +0300"

Excel How to convert datetime into only date - m/d/YYYY format

Date column has date and time in different formats.
05-09-14 21:58
11-08-13 11:56
08/19/2016 11:08:46
11-08-13 11:56
11-08-13 12:16
05/24/2014 08:26:06
08/24/2016 11:00:29
12/20/2014 09:16:19
08/25/2016 09:38:22
08/24/2016 10:59:05
08/25/2016 12:36:33
08/19/2016 10:38:37
11-08-13 14:53
11-08-13 16:18
11-08-13 13:38
10-10-13 16:14
11-08-13 12:44
08/31/2016 17:13:57
I'm trying to convert these datetime into only date m/d/YYYY format.
I tried =TEXT(cellofdate, "m/d/YYYY") but i'm still getting time for some entries.
A date is just a number.
To the left of the decimal place is the date, to the right is the time.
=INT(A1) will return the whole number.
Your first example will display as 05/09/2014 00:00. All you need to do now is format the cell as a date without the time.
Edit: And read the post that #Ralph linked to - very informative.
If your concerned cell is A1, you can use the following expression :
=DATEVALUE(MONTH(A1) & "/" & DAY(A1) & "/" & YEAR(A1))

Making Excel Recognize Custom Day/Time Format

I'm working with a date and time format from the Twitter API. It looks like this:
Tue Nov 26 20:44:15 +0000 2013
Is there a formula to convert this to a format that could be sorted chronologically? I don't need the +0000. Also not concerned about the day of week.
=DATEVALUE(MID(A1,9,3) & MID(A1,4,5) & RIGHT(A1,4)) + TIMEVALUE(MID(A1,11,9))
Then format as you like. As requested you would want a Custom Format of mmm dd HH:mm yyyy
Try this, assuming that your string is in A1:
=DATETIME(MID(A1, 5, 16) & MID(A1, 27, 4))
The two MID formulas cut out the parts of the string you want, namely the month, day, time, and year, but exclude the day of the week and the timezone. This produces a string that the DATETIME function can automatically recognize and covert into a native Excel date format.

Find out the time since unix epoch for a certain date time?

I want to find out the time in unix time (ie seconds since the unix epoch) on 9:00 BST on 1st October 2009. How can I do this on the linux command line?
I know you can use date #$UNIXTIME '+%someformat', but the unix time is what I'm trying to figure out
Using date thus:
date --date="Oct 1 09:00:00 BST 2009" +%s
Yields:
1254384000
date +%s
gives seconds since the epoch
Wikipedia (Unix Time) has
To show the time in seconds since 1970-01-01 (Unix epoch):
date +"%s" -d "Fri Apr 24 13:14:39 CDT 2009"
1240596879
I couldn't see your preferred date while I was editing this answer, so I didn't try it out -- but the example I found looks like a similar format.
It should work for all cases:
date +%s
I use this website: http://www.epochconverter.com/ to convert unixtime to human readable and vice versa. Even though it's not code, it's useful for validating your code. Here's some code (in Java) to do what you asked. The unixtime it prints out is an hour off according to epochconverter.com (not sure why).
SimpleDateFormat sdf = new SimpleDateFormat( "MM/dd/yyyy HH:mm z" );
try {
Date d = sdf.parse("10/01/2009 09:00 BST");
System.out.println("Unixtime is: " + d.getTime() / 1000);
} catch (ParseException pe) { pe.printStackTrace();

Resources