Covert in C# DateTime from US to UK format - c#-4.0

string DateAndTime = Cells[1].Text; // Output is 3/18/2020 3:00:18 PM
DateTime DT = DateTime.ParseExact(DateAndTime, "dd/MM/yyyy HH:mm:ss", CultureInfo.CurrentCulture);
Error: string was not recognized as a valid datetime
Current string is this 3/18/2020 3:00:18 PM
I want to convert and parse it to DateTime as 18/03/2020 15:00:18

ParseExact does exactly that, parses the string using the exact specification you provide. And, per your specification, "18" isn't a valid month. It sounds like you want to swap the month and day identifiers (and only use M instead of MM for the month, and use h for the single-digit 12-hour clock, and add tt for the AM/PM specification):
DateTime.ParseExact(DateAndTime, "M/dd/yyyy h:mm:ss tt")
Once it's parsed as a DateTime you can output the value in any format you like. For example:
DT.ToString("dd/MM/yyyy HH:mm:ss")
But your input format very much is not "dd/MM/yyyy HH:mm:ss". For parsing you need to match the input format, not the intended downstream format.

DateTime DT = DateTime.Parse(DateAndTime, new CultureInfo("en-US"));

Related

Excel - How to convert US date string (dd-mmm-yyyy) to European date

How can date in the US format dd-mmm-yyyy be converted to a date in European format.
When importing text based data with US Dates, some dates will import as a date, some import as a string.
A US date like 11-Jun-2021 is equal to NL format, so it will import as a date.
A US date like 11-Oct-2021 should be converted to 10-okt-2021, but it's imported as a string.
Is there a way to convert this string to a date format?
Found something. It's kinda dirty, but it works:
=IF(C3=0;"";IF(IFERROR(FIND("Oct";C3;1);FALSE);REPLACE(C3;FIND("Oct";C3;1);3;"Okt");IF(IFERROR(FIND("May";C3;1);FALSE);REPLACE(C3;FIND("May";C3;1);3;"Mei");IF(IFERROR(FIND("Mar";C3;1);FALSE);REPLACE(C3;FIND("Mar";C3;1);3;"Mrt");C3))))
This works for me in Italian. A3 contains the original date (only 8 months need to be translated):
=IF(ISNUMBER(A3);A3;1*SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(A3;"Jan";"GEN");"May";"MAG");"Jun";"GIU");"Jul";"LUG");"Aug";"AGO");"Sep";"SET");"Oct";"OTT");"Dec";"DIC"))

Date export from Matlab to excel

i have a set of data for couple of days and the names of the data files like this
name='Newyork20200915'
which is for the 15th of September and i want to export only the date to excel like shown below
So how can i get the date from the name string ?
Thanks in advance
Assuming that the other part of name will not contain any digits besides the date, you can use regexp to get all the digits from the character array:
name = 'Newyork20200915'
date_only = regexp(name, '\d*', 'match')
Next, you can convert this date string to a serial date number using datenum, by providing the format in which the date is currently. And then use datestr to format it to your desired format.
date_formatted = datestr(datenum(date_only, 'yyyymmdd'), 'dd. mmm')
date_formatted =
'15. Sep'

How to convert the norway date format to date format using python

I have the Norwegian date format of the date, but don’t know how to convert it to the standard date format using Python.
I have converted the date format below to standard date format.
tir. 18. jun. 2019
I have tried using the locale date format, but that is not working.
This the expected output
18-07-2019
Use dateparser
Ex:
import dateparser
d = "tir. 18. jun. 2019"
print(dateparser.parse(d).strftime("%d-%m-%Y"))
Output:
18-06-2019

Python pandas: unify datetime column format from CSV input

I have a mixed format column in a dataframe from a pd.read_csv(). There is a lot of information out there about datetime handling but I didn't find anything for this specific problem:
2 datatime types:
Custom dd/mm/yyyy hh:mm that shows up in excel as such: 10/03/2018 07:18
General that shows up in Excel as such: 8/13/2018 2:28:34 PM
I used :
df.Last_Updated = pd.to_datetime(df['Last_Updated'])
df = df.sort_values('Last_Updated').drop_duplicates(['Name'], keep='last')
But I get a mixed bunch where the custom format returns as another datatime type :
yyyy-mm-dd hh:mm:ss and shows up in my Excel export as 2017-11-22 19:54:35
Upon checking it changes the dd/mm/yyyy hh:mm (02/09/2018 17:55:44) format to yyyy-mm-dd hh:mm:ss (2018-02-09 17:55:44) and since I have to perform an exclusion of the type 'older than' it causes errors; in this particular case, a computer that has it's last connection in September returns as having it in February.
Does anyone know a way to unify the datetime format?
Date format:
from notepad:
X = "10/2/2018 10:07:31 PM"
Y = "8/13/2018 2:28:34 PM"
from CSV (and by opening the .txt via Excel):
X = 10/02/2018 22:07 PM
Y = 8/13/2018 2:28:34 PM
after datetime applied in code:
X = 02/10/2018 22:07:31
Y = 13/08/2018 14:28:34

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