How to add one month in current month? - asp.net

Suppose the current month is "Oct". I want that it will add one month in current month i.e it will show "Nov". For this my code is written below but it gives exception that Input string was not in a correct format.
So please correct the code?
if (Convert.ToInt32(ddlMonth.SelectedIndex ) <= Convert.ToInt32(DateTime.Now.AddMonths(1).ToString()))
{
TotalBalanceUptoSelectedPreviousMonth();
}

This should probably work:
if (Convert.ToInt32(ddlMonth.SelectedIndex )
<= Convert.ToInt32(DateTime.Now.AddMonths(1).ToString("M")))
TotalBalanceUptoSelectedPreviousMonth();
However, it looks simpler like this:
if (Convert.ToInt32(ddlMonth.SelectedIndex) <= DateTime.Now.AddMonths(1).Month)
Total...();

Convert.ToInt32(DateTime.Now.AddMonths(1).ToString())
probably gives the exception since DateTime.Now.AddMonths(1).ToString() value is not castable to Int32.

I'll go out on a limb and say that this is what you're looking for:
if(ddlMonth.SelectedIndex <= DateTime.Now.AddMonths(1).Month)
{
TotalBalanceUptoSelectedPreviousMonth();
}
Instead of getting the date as a string and converting it, why not use the Month property of the DateTime struct (which is already an integer)?
(Oh...and SelectedIndex is already an integer as well, no need for the call to Convert)

You need to use DateTime.Now.AddMonths(1).Month.
Also you do not need to use the ToString method wrapped in a Convert.ToInt32 method as this is already an integer.

Related

Xpages Date Time

I'm going loopy....
I want a date, in date format, for example
21/06/2017 17:23:04 GDT
I stamp this on a document, but I then want to display it on my xpage as:
21/06/2017 17:23
But I keep getting different results no matter what I do. I get the date from the onClick of a button using
var dt = new Date();
I then pass this into a function:
function AddObjectivesHistoryItem(doc, dt, action, username){
var ArrDocHistory:array = doc.getItemValueArray("History");
if(ArrDocHistory.length < 1){
// This should always return an object as it is created when an objectives document is first
// created but do this check to be safe and create an array if for some reason it doesnt exist
ArrDocHistory = [dt+"|"+action+"|"+username];
}else{
// append new value to the array
ArrDocHistory.push(dt+"|"+action+"|"+username);
}
doc.replaceItemValue("History",ArrDocHistory);
doc.replaceItemValue("LastUpdatedByName",username);
doc.replaceItemValue("LastUpdatedDate",dt);
}
I've tried using toLocaleString() and all others it seems but it wont work.
For example, toLocaleString() displays as 13-Mar-2018 15:02:15 on my xpage. It's close to what I want except it uses hyphens instead of slashes, and also displays the seconds.
I've tried using custom date pattern on my date field properties with no luck and I'm certain I'm missing something super obvious!?
Any pointers on how to firstly get the date like 21/06/2017 17:23:04 GDT and store as a date and secondly to then display it as 21/06/2017 17:23, this can be a string if it needs to be.
Thanks
You can get your date value as String in SSJS with:
var dateTimeFormat = new java.text.SimpleDateFormat("dd/MM/yyyy kk:mm");
var dateTimeString = dateTimeFormat.format(dt)));
If you want to store as text, java.text.SimpleDateFormat is best for converting a date server-side to a specific text format. It can also be used in a converter to manipulate to/from as well.

Parse alfresco date

I'm developing a custom validator of a date input in my workflow form and I get a null after parsing a date this is what I done:
// check dates can be parsed
str_expiryDate = field.form.prop_wfbxTestWorkFlow_NfDate.value;
console.log("Non conformite"+str_expiryDate);
str_reminderDate = field.form.prop_bpm_workflowDueDate.value;
console.log("echeance"+str_reminderDate);
Alfresco.logger.warn("Expiry Date: " + str_expiryDate + " | Reminder Date: " + str_reminderDate);
d_expiryDate = Date.parse(str_expiryDate);
console.log("nfDate"+str_expiryDate);
d_reminderDate = Date.parse(str_reminderDate);
console.log("Date echéance"+d_reminderDate);
and then i get this in console:
Non conformite2013-06-21T00:00:00.000+01:00 echeance2013-06-09T00:00:00.000+01:00
nfDatenull
Date echéancenull
How I can parse these two dates and then compare it? .thanks
Use Alfresco.util.fromISO8601(date)
According to the client-api docs
Convert an ISO8601 date string into a JavaScript native Date object
You are parsing the "value" of a date, not the date itself.
The best way to compare is, imho, using the format YYYYMMDD, and than compare it as a number.
Something like this (there is sure a far more elegant way to do that, but at this time it's the only one that got me):
var indexDate=str_expiryDate.indexOf("-");
var dayDate=str_expiryDate.substring(0, 2);
var monthDate=str_expiryDate.substring(3, 5);
var yearDate=fromData.substring(6, str_expiryDate.length+1);
int dataNew=yearDate+monthDate+dayDate;
and than compare the two dates value.
Obviously check if the index value are correct, I didn't double checked them.
Hope il helps.

How do I validate a string to be a date in the format dd-M-y, and not be a past date?

I have a date field and the format is "dd-M-y", example 01-Jan-2013. First I want to check the format which must be "dd-M-y" and secondly the date shouldn't be in the past but can be today and onward.
How would I do that? I would like to use regular expressions but I don't know what a suitable one would be.
You should use DateTime.TryParseExact rather than using Regex to validate your DateTime
string testDate = "01-Jan-2013";
DateTime temp;
if (DateTime.TryParseExact(testDate,
"dd-MMM-yyyy",
CultureInfo.InvariantCulture,
DateTimeStyles.None,
out temp) &&
(temp > DateTime.Today))
{
//Valid date and greater than today
}
else
{
//Invalid date or less than today
}
I think you should bind the user to fill the date in correct format instead of checking for it...
The Best solution in this case would be MaskEditExtender

Flex: Converting DateField text to seconds?

Is there anything wrong with following snippet of code?
var d:Date = DateField.dateToString(myDateField.text,"DD/MM/YYYY");
testTextArea.text = d.getSeconds().toString();
Error: Implicit coercion of a value of
type String to an unrelated type Date.
Here is your problem: DateField.dateToString's first parameter is supposed to be a date. It then takes that date and returns a string using the second parameter as a format string.
It looks like you're trying to convert the string to a date (the other way around) so you can get the seconds from it and put it in the text area. The DateField control has a selectedDate parameter that will give you the date you need. Then you just run this code to put it in the text area:
testTextArea.text = myDateField.selectedDate.getSeconds().toString();

How can I tell if E4X expression has a match or not?

I am trying to access an XMLList item and convert it to am XML object.
I am using this expression:
masonicXML.item.(#style_number == styleNum)
For example if there is a match everything works fine but if there is not a match then I get an error when I try cast it as XML saying that it has to be well formed. So I need to make sure that the expression gets a match before I cast it as XML. I tried setting it to an XMLList variable and checking if it as a text() propertie like this:
var defaultItem:XMLList = DataModel.instance.masonicXML.item.(#style_number == styleNum);
if(defaultItem.text())
{
DataModel.instance.selectedItem = XML(defaultItem);
}
But it still give me an error if theres no match. It works fine if there is a match.
THANKS!
In my experience, the simplest way to check for results is to grab the 0th element of the list and see if it's null.
Here is your code sample with a few tweaks. Notice that I've changed the type of defaultItem from XMLList to XML, and I'm assigning it to the 0th element of the list.
var defaultItem:XML =
DataModel.instance.masonicXML.item.(#style_number == styleNum)[0];
if( defaultItem != null )
{
DataModel.instance.selectedItem = defaultItem;
}
OK I got it to work with this:
if(String(defaultItem.#style_number).length)
Matt's null check is a good solution. (Unless there is the possibility of having null items within an XMLList.. probably not, but I haven't verified this.)
You can also check for the length of the XMLList without casting it to a String:
if (defaultItem.#style_number.length() > 0)
The difference to String and Array is that with an XMLList, length() is a method instead of a property.

Resources