How to convert YYYYMMDDHHMMSS to a date readable by `date`
date doesn't allow "YYYYMMDDHHMMSS", but it does "YYYYMMDD HH:MM:SS", so:
D="20100101123456"
date -d "${D:0:8} ${D:8:2}:${D:10:2}:${D:12:2}"
Converting YYYYMMDDHHMMSS to a date and time class in r
In Base R:
data.frame(datetime2=as.POSIXct(as.character(df$datetime),
format="%Y%m%d%H%M%S"),
cycle)
Output:
datetime2 cycle
1 2021-08-30 10:20:55 25
2 2021-08-30 10:23:12 30
3 2021-08-30 10:23:35 35
The key here is as.POSIXct
with format
:
> as.POSIXct(as.character(df$datetime), format="%Y%m%d%H%M%S")
[1] "2021-08-30 10:20:55 CST" "2021-08-30 10:23:12 CST" "2021-08-30 10:23:35 CST"
>
unix yyyymmddhhmmss format conversion to specific date format
The internal string conversion functions are too limited, so we use sed
and tr
when needed.
## The "readable" format yyyy.mm.dd.hh.mm.ss isn’t understood by date.
## yyyy-mm-dd hh:mm:ss is. So we first produce the latter.
# Note how to extract the last 14 characters of ${folder} and that, since
# we know (or should have checked somewhere else) that they are all digits,
# we match them with a simple dot instead of the more precise but less
# readable [0-9] or [[:digit:]]
# -E selects regexp dialect where grouping is done with simple () with no
# backslashes.
d="$(sed -Ee's/(....)(..)(..)(..)(..)(..)/\1-\2-\3 \4:\5:\6/'<<<"${folder:(-14)}")"
# Print the UTF date (for Linux and other systems with GNU date)
date -u -d "$d"
# Convert to your preferred "readable" format
# echo "${d//[: -]/.}" would have the same effect, avoiding tr
tr ': -' '.'<<<"$d"
For systems with BSD date
(notably MacOS), use
date -juf'%Y-%m-%d %H:%M:%S' "$d"
instead of the date
command given above. Of course, in this case the simplest way would be:
# Convert to readable
d="$(sed -Ee's/(....)(..)(..)(..)(..)(..)/\1.\2.\3.\4.\5.\6/'<<<"${folder:(-14)}")"
# Convert to UTF
date -juf'%Y.%m.%d.%H.%M.%S' "$d"
Convert date from yyyymmddHHMMSS format
You can import as a structured array as follow:
mydate, myvar = np.loadtxt('infile.txt', comments="#", skiprows=1, usecols=(0,4), unpack=True, dtype=[('date', '|S10'), ('floatmio', float)])
it will import the date as str
in mydate
array. And then you can use datetime
library on the single array entries to handle them as follow:
import datetime
dates = [datetime.datetime.strptime(x, '%Y%m%d%H') for x in mydate]
Convert 'yyyymmddhhmmss' to MM/DD/YYYY HH:MM (AM/PM) in Snowflake
Please check below select try_to_timestamp('20220120035900', 'YYYYMMDDHHMISS');
UTC to CST:
SELECT try_to_timestamp('20220120035900', 'YYYYMMDDHHMISS') as t_str,
t_str::timestamp_ntz as UTC_tz,
convert_timezone('UTC','America/Chicago', UTC_tz) chicago_time;
How to convert date-time string in the form (yyyymmddhhss) to dd/mm/yyyy in Javascript?
You could try using the Luxon module (an evolution of Moment.js), it will do this kind of thing very nicely:
const { DateTime } = luxon;
const timestamp = "20201127075434";
const dt = DateTime.fromFormat(timestamp, "yyyyMMddHHmmss");
console.log("dd/mm/yyyy:", dt.toFormat("dd/MM/yyyy"));
console.log("hh:mm:ss:", dt.toFormat("HH:mm:ss"));
<script src="https://cdnjs.cloudflare.com/ajax/libs/luxon/1.25.0/luxon.min.js" integrity="sha512-OyrI249ZRX2hY/1CAD+edQR90flhuXqYqjNYFJAiflsKsMxpUYg5kbDDAVA8Vp0HMlPG/aAl1tFASi1h4eRoQw==" crossorigin="anonymous"></script>
Convert a string with 'YYYYMMDDHHMMSS' format to datetime
You can use the STUFF()
method to insert characters into your string to format it in to a value SQL Server will be able to understand:
DECLARE @datestring NVARCHAR(20) = '20120225143620'
-- desired format: '20120225 14:36:20'
SET @datestring = STUFF(STUFF(STUFF(@datestring,13,0,':'),11,0,':'),9,0,' ')
SELECT CONVERT(DATETIME, @datestring) AS FormattedDate
Output:
FormattedDate
=======================
2012-02-25 14:36:20.000
This approach will work if your string is always the same length and format, and it works from the end of the string to the start to produce a value in this format: YYYYMMDD HH:MM:SS
For this, you don't need to separate the date portion in anyway, as SQL Server will be able to understand it as it's formatted.
Related Reading:
STUFF (Transact-SQL)
The STUFF function inserts a string into another string. It deletes a specified length of characters in the first string at the start position and then inserts the second string into the first string at the start position.
STUFF ( character_expression , start , length , replaceWith_expression )
unable to convert string date in Format yyyyMMddHHmmss to DateTime dart
intl DateFormat
can't cope with your input string as it doesn't have any separators. The whole string gets consumed as the year. However DateTime.parse
does cope with this (nearly). It happens to expect precisely the format you have (again, nearly).
One of the acceptable styles to parse
is 20120227T132700
, which just differs by the T
date/time separator.
Try this:
String date = '20180626170555';
String dateWithT = date.substring(0, 8) + 'T' + date.substring(8);
DateTime dateTime = DateTime.parse(dateWithT);
Convert String value format of YYYYMMDDHHMMSS to C# DateTime
Define your own parse format string to use.
string formatString = "yyyyMMddHHmmss";
string sample = "20100611221912";
DateTime dt = DateTime.ParseExact(sample,formatString,null);
In case you got a datetime having milliseconds, use the following formatString
string format = "yyyyMMddHHmmssfff"
string dateTime = "20140123205803252";
DateTime.ParseExact(dateTime ,format,CultureInfo.InvariantCulture);
Thanks
Related Topics
How Would One Disable Nagle's Algorithm in Linux
Get Ceiling Integer from Number in Linux (Bash)
How Does Bash Deal with Nested Quotes
How to Use Xargs to Copy Files That Have Spaces and Quotes in Their Names
How to Perform a For-Each Loop Over All the Files Under a Specified Path
How to Set a Custom Baud Rate on Linux
Redirecting Stdout with Find -Exec and Without Creating New Shell
How to Check If a Key Was Pressed in Linux
Tar Archiving That Takes Input from a List of Files
Relinking an Anonymous (Unlinked But Open) File
Differencebetween Double-Ampersand (&&) and Semicolon (;) in Linux Bash
Maximum Number of Concurrent Connections on a Single Port (Socket) of Server
Generating a Sha-256 Hash from the Linux Command Line
Colour Highlighting Output Based on Regex in Shell