How to print only year from a DataTable(Excel)

Hy Folks. Thanks in advance.

i wants to print only year from given data. please don’t send reference i’ve used all didn’t worked at all. i hope you will me give the correct code. PFA (Excel).Once again thanks

only year.xlsx (12.0 KB)

Thanks&Regards
NaNi

It would be great, if you could show us what you have tried so far? if any error received, show that too.

Hey @THIRU_NANI
you may try this

image

Thanks.

Thanks for your fast reply.

i created a veriable named “year”. i used

year=DateTime.Now.ToString("yyyy")

but it is only giving me the current year.
and also i’ve used

CultureInfo.CurrentCulture.Calendar.GetWeekOfYear(new DateTime(2019, 12, 02) , currentCulture.DateTimeFormat.CalendarWeekRule, currentCulture.DateTimeFormat.FirstDayOfweek)

which is giving me the correct year but i don’t wants to mentioned the date.
without mentioning the date and it should take the data from excel file. and it should print only year.

change data type of “Year” to string

Hi @THIRU_NANI

Please use the below method.

string strIn = row("Columnname").tostring   
dateTime dt = DateTime.ParseExact(strIn, "dd-MM-yyyy",System.Globalization.CultureInfo.InvariantCulture)
string strOut = dt.ToString("yyyy")

@THIRU_NANI - I didn’t use Preserve Format while reading, just to see how the value is read from the excel. Then in the next line I have used Datetime.parseexact to extract the year…

Hi @THIRU_NANI ,

Try below code and let me know if you face any issue.
Refer Output sheet for output
test.xaml (6.8 KB)
Test.xlsx (10.2 KB)

Thank you so much guys for your fast response. I’ll check the codes tomorrow.

Please can you also tell me how to find out the week number from the same date.

It would be helpful.

Thanks a lot guys @sai_krishna_somisetty @prasath17 @pravin_calvin @ermanoj3101

Thanks and regards
NaNi

Here you go…

CultureInfo.InvariantCulture.Calendar.GetWeekOfYear(datetime.ParseExact(CurrentRow(0).ToString,"MM/dd/yyyy hh:mm:ss",System.Globalization.CultureInfo.InvariantCulture), CalendarWeekRule.FirstDay,DayOfWeek.Monday).ToString

You can change calendarweekRule based on your requirement…

Hi @THIRU_NANI ,

If you don’t want unique years and only want to extract years then remove highlighted code from shared code and then run the bot.

Hi @sai_krishna_somisetty it is working

Thanks buddy

Thanks&Regards
NaNi

1 Like

Hi @prasath17 PFS below

Hi @THIRU_NANI

To get the week number, using this code

“System.Globalization.CultureInfo.CurrentCulture.Calendar.GetWeekOfYear(Cdate(CurrentRow(0)),CalendarWeekRule.FirstDay,DayOfWeek.Monday)”

Assign this to a interger variable, as it gives integer result.

Thanks.

1 Like

Hi @sai_krishna_somisetty ,

Thank you so much for your help

Thanks&Regards
NaNi

@THIRU_NANI - In the Import Tab…you have just to add “System.Globalization” that’s all.

This is code is just a shorter version.

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.