Convert Date String from YYYYMMDD to DD Mon YYYY
You will need to parse your string into something that Date can understand. How about:
var dateString = "20180619";
var parsedDate = dateString.replace(/(\d{4})(\d{2})(\d{2})/, '$1/$2/$3');
var date = new Date(parsedDate);
var d = date.getDate();
var months = ['Jan', 'Feb', 'Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
var m = months[date.getMonth()];
var y = date.getFullYear();
console.log(d + ' ' + m + ' ' + y)
The result is 19 Jun 2018
how to convert YYYYMMDD to DD-Mon-YYYY in oracle?
Use this combination of to_char
and to_date
:
select to_char (to_date('20150324','YYYYMMDD'), 'DD-Mon-YY') from dual;
Your mistake was, that you used the wrong date pattern. Additionally it's recommended to add''
, though it worked without them in this case.
Check this Fiddle.
Cast-or-convert yyyy-mm-dd to dd-mon-yyyy am/pm
First of all - the best advice I can give you is to stop using string representations of DateTime values.
There's only one place where it's good to use a string representation of a dateTime and that's at the presentation level - the UI that use the data.
Having said that, sometimes it's impossible to change the implementation of a system and you're just stuck with what you have - so here's a code example of how to convert a string containing 2018-12-07 13.00.54.984000
to a string containing 07-DEC-18 01.00.54.984000 PM
.
So the first step would obviously be to convert the source string to DateTime2
- but for that you need to replace the dots between the hours minutes and seconds with colons.
You can use STUFF
to do that.
DECLARE @DateString varchar(30) = '2018-12-07 13.00.54.984000'
SELECT CAST(STUFF(STUFF(@DateString, 17, 1, ':'), 14, 1, ':') AS DateTime2);
Now, to get the desired string format from DateTime2
it's simplest to use the Format
function (though it's worth mentioning it's performance is pretty slow compared to other conversion options - see Aaron Bertrand's FORMAT() is nice and all, but…)
SELECT FORMAT(@DateTime, 'dd-MMM-yy hh.mm.ss.fffffff tt')
The entire script:
DECLARE @DateString varchar(30), @Date DateTime2;
SET @DateString = '2018-12-07 13.00.54.984000'
SELECT @Date = CAST(STUFF(STUFF(@DateString, 17, 1, ':'), 14, 1, ':') AS DateTime2)
SELECT FORMAT(@Date, 'dd-MMM-yy hh.mm.ss.fffffff tt') As Result
Result
07-Dec-18 01.00.54.9840000 PM
Python: convert date format YYYY-mm-dd to dd-MON-yyyy with abbreviated month
Just use upper()
to capitalize the output string:
from datetime import datetime
x = datetime.strptime('2021-01-16', '%Y-%m-%d')
print(x.strftime('%d-%b-%Y').upper())
# 16-JAN-2021
How to convert yyyy-mm-dd to dd-Mon-yyyy (with month name) using javascript
Try this
function formatDate(d) { var date = new Date(d);
if ( isNaN( date .getTime() ) ) { return d; } else { var month = new Array(); month[0] = "Jan"; month[1] = "Feb"; month[2] = "Mar"; month[3] = "Apr"; month[4] = "May"; month[5] = "Jun"; month[6] = "Jul"; month[7] = "Aug"; month[8] = "Sept"; month[9] = "Oct"; month[10] = "Nov"; month[11] = "Dec";
day = date.getDate(); if(day < 10) { day = "0"+day; } return day + " " +month[date.getMonth()] + " " + date.getFullYear(); } }
date_response = formatDate('2016-10-25');
alert(date_response)
converting string to date format like dd-mon-yyyy
Do you mean something like this?
Console.WriteLine(Strings.Format(Date.ParseExact("20200731", "yyyyMMdd", System.Globalization.DateTimeFormatInfo.InvariantInfo), "dd-MMM-yyyy").ToUpper)
How to convert date format from yyyy-mm-dd to dd MMM yyyy
You can use the following method to convert date format simply:
public string ConvertToOutputDate(string input)
{
return DateTime.Parse(input).ToString("dd MMM yyyy");
}
And also you can play with other date formats by passing them as args to ToString() method.
Convert into DD-MON-YYYY format in oracle
Your dates are not dates but timestamps with a time zone. Don't use TO_DATE
because you will be discarding a lot of information and potentially getting the wrong date when you discard the information. Use TO_TIMESTAMP_TZ
instead.
SELECT TO_TIMESTAMP_TZ( your_column, 'YYYY-MM-DD"T"HH24:MI:SS.FF3TZHTZM' )
FROM your_table
Then if you really want to naively convert the timestamp to a string and discard the timezone information without applying it to the date then just use TO_CHAR
:
SELECT TO_CHAR(
TO_TIMESTAMP_TZ( your_column, 'YYYY-MM-DD"T"HH24:MI:SS.FF3TZHTZM' ),
'DD-MON-YYYY',
'NLS_DATE_LANGUAGE=AMERICAN'
)
FROM your_table
However, you probably want to normalise the time-zone and convert all the timestamps to a common time-zone (i.e. UTC) and need to use AT TIME ZONE 'UTC'
:
SELECT TO_CHAR(
TO_TIMESTAMP_TZ( your_column, 'YYYY-MM-DD"T"HH24:MI:SS.FF3TZHTZM' )
AT TIME ZONE 'UTC',
'DD-MON-YYYY',
'NLS_DATE_LANGUAGE=AMERICAN'
)
FROM your_table
So, for some test data:
CREATE TABLE your_table ( your_column ) AS
SELECT '2020-07-05T00:00:00.000+0000' FROM DUAL UNION ALL
SELECT '2020-07-05T00:00:00.000+0100' FROM DUAL UNION ALL
SELECT '2020-07-04T23:00:00.000-0100' FROM DUAL;
Then the options above:
SELECT TO_TIMESTAMP_TZ( your_column, 'YYYY-MM-DD"T"HH24:MI:SS.FF3TZHTZM' )
AS timestamp,
TO_CHAR(
TO_TIMESTAMP_TZ( your_column, 'YYYY-MM-DD"T"HH24:MI:SS.FF3TZHTZM' ),
'DD-MON-YYYY',
'NLS_DATE_LANGUAGE=AMERICAN'
) AS ignore_timezone,
TO_CHAR(
TO_TIMESTAMP_TZ( your_column, 'YYYY-MM-DD"T"HH24:MI:SS.FF3TZHTZM' )
AT TIME ZONE 'UTC',
'DD-MON-YYYY',
'NLS_DATE_LANGUAGE=AMERICAN'
) AS utc_date
FROM your_table
Will output:
TIMESTAMP | IGNORE_TIMEZONE | UTC_DATE
:--------------------------- | :-------------- | :----------
2020-07-05T00:00:00.000+0000 | 05-JUL-2020 | 05-JUL-2020
2020-07-05T00:00:00.000+0100 | 05-JUL-2020 | 04-JUL-2020
2020-07-04T23:00:00.000-0100 | 04-JUL-2020 | 05-JUL-2020
db<>fiddle here
Related Topics
How to Refresh the Data in a Jqgrid
How to Remove HTML Element from Dom With React
React State Change Not Causing Re-Render
Es6/Babel Class Constructor Cannot Be Invoked Without 'New'
How to Pass JavaScript Confirm Variable to PHP Variable
Simple Pagination in JavaScript
Finding the Symmetric Difference Between Two Arrays
How to Make a Triangle Using for Loop JavaScript
How to Fetch Url of Current Tab in My Chrome Extension Using JavaScript
Uncaught Typeerror: (Intermediate Value)(...) Is Not a Function
Json to CSV Flattening Nested Json
Replace All Chars With #, Except for Last 4
React .Map() Is Not a Function Error
How to Resolve the C:\Fakepath
Cannot Read Property of Undefined @Viewchild Not Working Angular 5
Javascript: Refresh Same Page With Query String from Selects