Month Name as a String

Get month name from Date

Shorter version:

const monthNames = ["January", "February", "March", "April", "May", "June",  "July", "August", "September", "October", "November", "December"];
const d = new Date();document.write("The current month is " + monthNames[d.getMonth()]);

Month name as a string

Use getDisplayName.

For earlier API's use String.format(Locale.US,"%tB",c);

Extract month name from raw string?

You could import the month names from the built-in calendar module and also use a generator instead of a list comprehension for better efficiency:

>>> from calendar import month_name
>>> s = 'January 2045 Robots'
>>> months = {m.lower() for m in month_name[1:]} # create a set of month names
>>> next((word for word in s.split() if word.lower() in months), None)
'January'

Alternatively, you could use a regular expression:

>>> from calendar import month_name
>>> import re
>>> pattern = '|'.join(month_name[1:])
>>> re.search(pattern, s, re.IGNORECASE).group(0)
'January'

Get month name from month number for a series of numbers

you can get String description of a month from int using 1) array of month or 2) own method.

Case 1:

String[] monthString = new String[]{"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};

Later, in the code, you can call it like:
EXAMPLE:

 for(int i=0; i<12; i++) {
System.out.println("Month: "+monthString[i]);
}

Assuming, we have case when we need to get month some times (5,4,3,2);

...(monthString[5], monthString[4], monthString[3], monthString[2]) ..

Case 2:

   public String getMonth(int month){
String monthString;
switch (month) {
case 1: monthString = "January";
break;
case 2: monthString = "February";
break;
case 3: monthString = "March";
break;
case 4: monthString = "April";
break;
case 5: monthString = "May";
break;
case 6: monthString = "June";
break;
case 7: monthString = "July";
break;
case 8: monthString = "August";
break;
case 9: monthString = "September";
break;
case 10: monthString = "October";
break;
case 11: monthString = "November";
break;
case 12: monthString = "December";
break;
default: monthString = "Invalid month";
break;
}
return monthString;
}

EXAMPLE:

   for(int i=0; i<12; i++) {
System.out.println("Month: "+getMonth(i));
}

OUTPUT:

Month: January
Month: February
Month: March
Month: April
Month: May
Month: June
Month: July
Month: August
Month: September
Month: October
Month: November
Month: December

Convert String with month name to datetime

In Python, this is an easy exercise in using the datetime format strings:

from datetime import datetime

s = "8 March, 2017"
d = datetime.strptime(s, '%d %B, %Y')
print(d.strftime('%Y-%m-%d'))

See this table for a full description of all the format qualifiers.

Here I use the datetime.strptime method to convert your datepicker.js string to a Python datetime object. And I use the .strftime method to print that object out as a string using the format you desire. At first those format strings are going to be hard to remember, but you can always look them up. And they well organized, in the end.

I do wonder, though: might it be better to stay in JavaScript than switch over to Python for this last step? Of course, if you are using Python elsewhere in your process, this is an easy solution.

How do I convert month name in string to short month

for mysql you can use str_to_date() for convert a string in date

    select str_to_date('23-08-2018', 'd%-m%-%Y') 
from dual

then you you have a proper date value

and you can show a date value using date format

   select date_format( my_col_date, '%Y/%m/%d')
from dual

select date_format(str_to_date('23-08-2018', 'd%-m%-%Y'), '%Y/%m/%d')
from dual

In your case

   select date_format(STR_TO_DATE("12-Aug-2017", "%d-%b-%Y"), '%Y/%m/%d')
from dual

see this refer for mask parameters https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_date-format

Assuming your insert table contain col1, col2, col3, .... col4
and col1 si column for date values
for load infile you should convert your date as

    LOAD DATA INFILE '/path/to/your_file.csv'
IGNORE INTO TABLE temp_test
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\r\n' #use '\n' for linux
IGNORE 1 LINES
(@var_col1, col2, col3, ......, coln)
SET col1 = STR_TO_DATE(@var_col1,'%d-%m-%Y');


Related Topics



Leave a reply



Submit