so I have a variable containing a date object. I want to convert it to a string in this format: dd/mm/yyyy. How could this be achieved?
You can use Flex 3.5 DateFormatter to format the date.
var fmt:DateFormatter = new DateFormatter();
fmt.formatString = "DD/MM/YYYY";
return fmt.format(date);
Or you can write your own:
function format(date:Date):String {
function pad(n:int):String {
return return n<10 ? '0'+n : n;
}
return pad(date.getDate()) + "/" +
pad(date.getMonth() + 1) + "/" +
date.getFullYear();
}
toDateString() might do what you need, but this should also work:
Straight from here
function dateToMMDDYYYY(aDate:Date):String {
var SEPARATOR:String = "/";
var mm:String = (aDate.month + 1).toString();
if (mm.length < 2) mm = "0" + mm;
var dd:String = aDate.date.toString();
if (dd.length < 2) dd = "0" + dd;
var yyyy:String = aDate.fullYear.toString();
return dd + SEPARATOR + mm + SEPARATOR + yyyy;
}
Related
I have noticed that the script is significantly faster when I comment out the line that calls setValues(). Not that it's a super big issue, but I am interested in the different ways (if any) that I could speed the process up. If anyone has some insight, I would greatly appreciate it! It would also help me see where I could make similar optimizations in other parts of my script.
Here is the code in question:
// import new value(s)
if ((numRaw - numDk) > 0) {
var iDate;
var lastRow = getLastDataRow(earSheet, columnToLetter(earSheet.getRange("EAR_DATES_RNG").getColumn()));
var distName = impSheet.getRange("A1").getValue().toString();
var defaultDept = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("All Lists").getRange("A2").getValue().toString();
impData.forEach(function (entry) {
iDate = new Date(entry[0]);
var newEar = earSheet.getRange("A" + lastRow + ":G" + lastRow).getValues(); // init copy
if (currData.get(iDate.getTime()) == null) {
newEar[0][0] = entry[0];
newEar[0][1] = distName;
newEar[0][6] = entry[1];
newEar[0][3] = "Royalties";
newEar[0][4] = defaultDept;
newEar[0][5] = "NOT SPECIFIED";
earSheet.getRange("A" + lastRow + ":G" + lastRow).setValues(newEar);
Logger.log("ADDED: " + entry[1] + " to row: " + lastRow);
addedNewData++;
lastRow++;
}
else {
Logger.log("EXISTING DATA FOUND: " + currData.get(iDate.getTime()));
}
});
}
Using Matt and Cooper's suggestions, I came up with this revised code which is significantly faster:
if ((numRaw - numDk) > 0) {
var iDate;
var prevLastRow = getLastDataRow(earSheet, columnToLetter(earSheet.getRange("EAR_DATES_RNG").getColumn()));
var currLastRow = getLastDataRow(earSheet, columnToLetter(earSheet.getRange("EAR_DATES_RNG").getColumn()));
var rangeSize = numRaw;
var distName = impSheet.getRange("A1").getValue().toString();
var defaultDept = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("All Lists").getRange("A2").getValue().toString();
var newEar = earSheet.getRange("A" + prevLastRow + ":G" + (prevLastRow + rangeSize - 1)).getValues(); // init copy
impData.forEach(function (entry) {
iDate = new Date(entry[0]);
if (currData.get(iDate.getTime()) == null) {
newEar[currLastRow-2][0] = entry[0];
newEar[currLastRow-2][1] = distName;
newEar[currLastRow-2][6] = entry[1];
newEar[currLastRow-2][3] = "Royalties";
newEar[currLastRow-2][4] = defaultDept;
newEar[currLastRow-2][5] = "NOT SPECIFIED";
Logger.log("ADDED: " + entry[1] + " to row: " + currLastRow);
addedNewData++;
currLastRow++;
}
else {
Logger.log("EXISTING DATA FOUND: " + currData.get(iDate.getTime()));
}
});
earSheet.getRange("A" + prevLastRow + ":G" + (prevLastRow + rangeSize - 1)).setValues(newEar);
}```
I have a DataField with editable="true" and format mm/dd/yyyy. Then lets say user typed in month mm section 13 which is not correct. How can I validate it as well as dd section and yyyy section and show a pop up when it's incorrect?
Here is what happening when apply button was clicked:
var newDate:Date = dfDate.selectedDate;
var month:String = (newDate.month + 1) < 10 ? "0" + (newDate.month + 1).toString() : (newDate.month + 1).toString();
var date:String = newDate.date < 10 ? "0" + newDate.date.toString() : newDate.date.toString();
var year:Number = newDate.getFullYear();
var dateString:String = month + "/" + date + "/" + year;
Button section:
<mx:FormItem id="itemDate">
<mx:DateField id="dfDate" yearNavigationEnabled="true" editable="true"/>
</mx:FormItem>
You can use just a simple regular expression
var customRegExp:RegExp = new RegExp(/(0[1-9]|1[012])[\/](0[1-9]|[12][0-9]|3[01])[\/](19|20)\d\d/);
And then use .test function along with some formatting stuff
var fmt:DateFormatter = new DateFormatter();
fmt.formatString = "MM/DD/YYYY";
if(!customRegExp.test(fmt.format(dfDate.selectedDate)))
{
Alert.show("Please input date in format MM/DD/YYYY");
enableForm(true);
}
Worked for me just fine and I think will work for others as well!
Here is code all together:
var customRegExp:RegExp = new RegExp(/(0[1-9]|1[012])[\/](0[1-9]|[12][0-9]|3[01])[\/](19|20)\d\d/);
var fmt:DateFormatter = new DateFormatter();
fmt.formatString = "MM/DD/YYYY";
if(!customRegExp.test(fmt.format(dfDate.selectedDate)))
{
Alert.show("Please input date in format MM/DD/YYYY");
enableForm(true);
}
I am using the following code to redirect visitors on my website:
<meta charset="UTF-8">
<meta http-equiv="refresh" content="1;url=http://mylink.com">
<script type="text/javascript">
window.location.href = "http://mylink.com"
</script>
I would like to add dates to the link so that the link becomes this:
"http://mylink.com/startdate=2016-01-09;enddate=2016-02-09"
I would also like to set the startdate parameter set to tomorrow's date and enddate parameter to be calculated based on the startdate value.
Thanks in advance!
Although JavaScript is not tagged in your question, I would do something like this:
function formatDate(date) {
var days = date.getDate();
var months = date.getMonth() + 1;
var fixedDays = (days < 10)? ("0" + days) : days;
var fixedMonths = (months < 10)? ("0" + months) : months;
var result = (1900 + date.getYear()) + "-"
+ fixedMonths + "-" + fixedDays;
return result;
}
function createLink(baseUrl, daysExpiration) {
var tomorrow = new Date();
tomorrow.setDate(tomorrow.getDate() + 1);
var expire = tomorrow;
expire.setDate(expire.getDate() + daysExpiration);
var result = baseUrl +
"/startdate=" + formatDate(tomorrow) +
";enddate=" + formatDate(expire);
return result;
}
var link = createLink("www.ex.com", 30);
window.location = link;
Here's a JSFiddle.
My json string is coming as follows:
"[{\"StartTime\":\"09:00\",\"Dates\":\"05-28-2015\",\"Code\":\"DF\",\"LocationCode\":\"NY\"},{\"StartTime\":\"09:30\",\"Dates\":\"05-28-2015\",\"Code\":\"DF\",\"LocationCode\":\"NY\"},{\"StartTime\":\"10:00\",\"Dates\":\"05-28-2015\",\"Code\":\"DF\",\"LocationCode\":\"NY\"},{\"StartTime\":\"10:30\",\"Dates\":\"05-28-2015\",\"Code\":\"DF\",\"LocationCode\":\"NY\"},{\"StartTime\":\"11:30\",\"Dates\":\"05-28-2015\",\"Code\":\"DF\",\"LocationCode\":\"NY\"}]"
I need to parse this json string on view and show the data in a table.
I am new to json. Any help will be really appreciated. Thanks.
$("#divLoad").load("GetAvailableTimeSlots?strProvider=" + provider + "&strFrom=" + from, function (data) {
var newStr = data.replace('"[', '').replace(']"', '').replace('[', '').replace(']', '');
var dataArr = newStr.split('},{');
var jsonArr = new Array(dataArr.length);
for (var i = 0; i < dataArr.length; i++) {
dataArr[i] = '{' + dataArr[i] + '}';
var dataElem = dataArr[i].replace('{{', '{').replace('}}', '}');
var jsonElem = "'" + dataElem + "'";
jsonArr[i] = JSON.parse(jsonElem);
}
$(this).html(jsonArr);
});
If you are using jQuery, you can use the jQuery.parseJSON() method.
http://api.jquery.com/jquery.parsejson/
Just call JSON.parse(data) and manipulate the javascript object instead of performing error-prone string manipulation.
function (data) {
var locations = JSON.parse(data);
var table = $("<ul>");
for (var i = 0; i < locations.length; i++) {
var row = $("<li>").text(locations[i].StartTime + " " + locations[i].Dates + " " ...);
table.append(row);
}
$("#divLoad").empty().append(table); // $(this) won't work
}
This example is using an unordered list but you can easily replace this with table markup.
Here's what it looks like in Chrome dev tools with console.log(locations) after parsing:
I have some code not written by me that I'm trying to compile.
public static function getUserInfoObject(info:Array) : Object {
var lastBattleTime:Number = info[7];
var listLength:Number = info[8];
var list:Array = info.slice(9,9 + listLength);
var achievesLength:Number = info[9 + listLength];
var achievements:Array = info.slice(10 + listLength,10 + listLength + achievesLength);
var statsLength:Number = info[10 + listLength + achievesLength];
var stats:Array = info.slice(11 + listLength + achievesLength,11 + listLength + achievesLength + statsLength);
var commonInfo:Array = info.slice(11 + listLength + achievesLength + statsLength,11 + listLength + achievesLength + statsLength + 8);
return
{
"uid":info[0],
"name":info[1],
"chatRoster":info[2],
"status":info[3],
"displayName":info[5],
"list":list,
"achievements":achievements,
"stats":stats,
"commonInfo":commonInfo,
"creationTime":App.utils.locale.longDate(info[6]),
"lastBattleTime":(lastBattleTime == 0?"":App.utils.locale.longDate(lastBattleTime) + " " + App.utils.locale.longTime(lastBattleTime))
};
}
It gives me this error: 1078: Label must be a simple identifier. in every line in return.
Am I blind or dumb or this code is bad?
You should start your return statement with the curly brace, not with new line:
public static function getUserInfoObject(info:Array) : Object {
return { // <-Here
};
}