How to get today date

Hi,

is there any frappe method to get today date with the right format ?

Thanks

1 Like

Are you referring to default value in forms, or python code?

both of them…actually now i need for the python code , nut in the near future i guess ill need in form value too

1 Like

Try this:

frappe.utils.nowdate()

7 Likes

How can I set today date in default? I try: frappe.utils.nowdate(), now(), datetime.now()… doesn’t work…

For Forms UI set Today as default in date field.

For Python Code

from frappe import utils
utils.now()
utils.today()
5 Likes

@BhupeshGupta, Adding “Today” into the form UI default field is a great tip. Thanks.

2 Likes

Is there any keyword for Forms UI default to get tomorrow date?

nopes, set the same via custom script

1 Like

@BhupeshGupta how to set input date to be 30 days after today by default? I want to default payment due date but it’s not fixed days… Thanks

1 Like

@jof2jc

frappe.datetime.add_days(date, days);   // add n days to a date
frappe.datetime.add_months(date, months); // add n months to a date
frappe.datetime.month_end(date);  // returns the first day from the month of the given date
frappe.datetime.month_start(date); // returns the last day from the month of the given date
frappe.datetime.get_day_diff(begin, end); // returns the days between 2 dates
18 Likes

@max_morais_dmm, where to put this? I puy it in default value under Customize Form but it thrown error…

Cab you help with example? Thnks a lot

Why you don’t use “Credit Days” functionality? You can define Credit Days in Customer record.

1 Like

If you are customising a form and wants to set default value for date field to today then use “Today” in default section.

@jof2jc you need put it in a custom script!

frappe.ui.form.on('Sales Order', 'onload', function(frm){
    frm.set_value("delivery_date", frappe.datetime.add_days(frappe.datetime.nowdate(), 20));
})
10 Likes

@max_morais_dmm Hi I would really appreciate your help with this. Can I fetch the number of days to add in field from a custom field? Let’s say I would like to enter the credit days manually in each sales, so I did this:

  1. add customer field called “credit_days”
  2. fetch it into the field from customer master like this to retrieve the standard credit days;

cur_frm.add_fetch(‘customer’,‘credit_days’,‘credit_days’)

  1. Add this script to calculate, but it adds 20 days as it was intended from your post.

frappe.ui.form.on(‘Sales Invoice’, ‘onload’, function(frm){
frm.set_value(“due_date”, frappe.datetime.add_days(frappe.datetime.nowdate(), 20));
})

Now it would be awesome if you could help change the script to fetch the days to add from the credit days field which is populated automatically from the customer master, but needs to be changed case by case, so when changed it calculates the new due date based on that field.

I am thankful for your help.
Mo

@mrmo this post will provide to you the knowledge for you achive you requirement

1 Like

@max_morais_dmm @nabinhait Hi again, I tried to check that post but I don’t see a way to update the date according to the value in custom field.
What I would like to do is enter for example 40 days in custom field ‘credit_days’ and after that the “payment due date field” should add 40 days to its current date.

frappe.ui.form.on(‘Sales Invoice’, ‘onload’, function(frm){
frm.set_value(“due_date”, frappe.datetime.add_days(frappe.datetime.nowdate(), row.credit_days));
})

I really appreciate your help, thank you.

@mrmo, define your logic based on customer field change!

You can achive this, because you dont have the customer while onload, so you cant call the server to get the credit_days from the customer.

1 Like

@max_morais_dmm I am thankful for your help, maybe I have explained it wrong, let me try again.
I would like to add days based on days that I input in the custom field on the sales invoice document, which I call credit days. I am sorry for the confusion, below is a picture with screenshot for your reference, thank you so much.