convert date from json format to other formats in sencha - sencha-touch

Can anyone tell me how to convert date from Json data to normal date format in sencha.
var df = this.dateFormat;
if (!v) {
return v;
}
if (Ext.isDate(v)) {
return v;
}
if (df) {
if (df == 'timestamp') {
return new Date(v * 1000);
}
if (df == 'time') {
return new Date(parseInt(v, 10));
}
return Date.parseDate(v, df);
}
var parsed = Date.parse(v);
return parsed ? new Date(parsed) : null;
Thanks in advance

The Ext.Date class is what you're looking for. Try something like:
var parsed = Ext.Date.parse(valueFromJSON, "Y-m-d g:i:s A");
The Ext.Date.parse method returns a Date.

Related

If else on Mono if empty or has value

This is the sample code
repo1.findById( id )
.map( p -> {
if( p == null ){
return repo2.findById( id ).flatMap( g -> {
g.setValue("some value");
return g;
});
}
else{
return repo3.findById( id ).flatMap( h -> {
h.setValue("some value");
return h;
});
}
});
Any better way to do this ?. If else inside flat map does not look neat to me.
The idiomatic approach would be to use the switchIfEmpty() operator.
You would only proceed to use the call to repo3 if repo1 actually returns a result.
If not data matches repo1.findById(id), then this call would return an empty result, not null.
To cover this case, use switchIfEmpty().
public Mono<Data> load(String id){
return repo1.findById(id)
.flatMap(p -> {
return repo3.findById(id)
.flatMap(h -> {
h.setValue("some value");
return h;
});
})
.switchIfEmpty(repo2.findById(id)
.flatMap(g -> {
g.setValue("some value");
return g;
}));
}

Date formatting in Karate

I have the the below date returned from the SQL query in Karate feature file:
2020-01-31 00:00:00.0
I need to convert it to: 31-JAN-20 format. I have tried the below:
* def effectiveDt =
"""
function(s) {
var SimpleDateFormat = Java.type('java.text.SimpleDateFormat');
var sdf = new SimpleDateFormat("dd-mon-yy");
return return sdf.format(s)
}
"""
but its not working for me.
but the below worked and returns 31-00-19, but I want 31-JAN-20 format
var sdf = new SimpleDateFormat("dd-mm-yy");
Any help would be appreciated!
Here is an example that worked for me:
* def getSubtractedYear =
"""
function(s) {
var DateTimeFormatter = Java.type("java.time.format.DateTimeFormatter");
var LocalDate = Java.type("java.time.LocalDate");
var ChronoUnit = Java.type("java.time.temporal.ChronoUnit");
var dtf = DateTimeFormatter.ofPattern("MM/dd/yyyy");
try {
var adj = LocalDate.parse('02/02/2020', dtf).minusMonths(12);
return dtf.format(adj);
} catch(e) {
karate.log('*** date parse error: ', s);
}
}
"""
And call like:
* string subtracted = call getSubtractedYear aDate

Function Doesn't Return a value in Dart Language

I am trying to connect my app to an API and search using the API. If the search is successful and found the name, it will return Success and if not, will return Failed, but it doesn't return anything.
import 'dart:convert';
import 'package:http/http.dart' as http;
main() {
getData();
}
String linearSearch(List<dynamic> list, String x) {
for (var i = 0; i < list.length; i++){
if(x == list[i]["name"]){
return 'Success';
}
}
return 'Failed';
}
void getData() async {
List data;
var response = await
http.get("http://localhost:3000/data");
data = jsonDecode(response.body);
linearSearch(data, 'karim');
}
Your function returns String value without any additions. You just need to assign result of your function to String variable and do something with it, if needed.
Or you just could print result of the function
void getData() async {
List data;
var response = await
http.get("http://localhost:3000/data");
data = jsonDecode(response.body);
print(linearSearch(data, 'karim'));
}
Solved by editing my linearSearch function
linearSearch(List<dynamic> list, String x) {
for (var i = 0; i < list.length; i++){
if(x == list[i]["name"]){
return print('Success');
}
}
return print('Failed');
}

Binary operator '-' cannot be applied to two (NSDate?) operators

Trying to get a total amount of time from a start time and end time.
I have the DateFormatter correct as in Extension.swift below but I am getting confused on the way to calculate bottomtime - starttime.
Any help is appreciated. Thanks
Extension.swift
extension NSDate{
var bottomtimestringValue: String{
return self.toString()
}
func tobottomtimeString() -> String {
let formatter = NSDateFormatter()
formatter.dateFormat = "yyyy-MMM-dd"
let str = formatter.stringFromDate(self)
return str
}
}
extension String{
var bottomtimedateValue: NSDate?{
return self.toDate()
}
func tobottomtimeDate() -> NSDate? {
let formatter = NSDateFormatter()
formatter.dateFormat = "yyyy-MMM-dd"
if let date = formatter.dateFromString(self) {
return date
}else{
// if format failed, Put some code here
return nil // an example
}
}
}
Adddivelogviewcontroller.swift
var a = (textFieldStartTime.text.starttimedateValue)
var b = (textFieldEndTime.text.endtimedateValue)
var sum = b - a
textFieldBottomTime.text = "\(sum)"
That's correct-- the - operator is not defined for NSDate?, so you can't find the difference that way. Since a and b are both NSDate?, you could find the difference like this:
if let dateA = a, dateB = b {
let difference = dateA.timeIntervalSinceReferenceDate - dateB.timeIntervalSinceReferenceDate
}
Here, difference's type will be NSTimeInterval?.
Or if you prefer, you could add a definition of - for NSDate?, which might look like this:
func -(lhs:NSDate?, rhs:NSDate?) -> NSTimeInterval? {
if let left=lhs, right=rhs {
return left.timeIntervalSinceReferenceDate - right.timeIntervalSinceReferenceDate
} else {
return nil
}
}
Add that and you can use - above.
OK, not really sure if thats working or not but i get no errors. What I need this to do calculate after the end time picker OK botton is pressed
func OK3ButtonTapped(sender: UIBarButtonItem) {
self.textFieldEndTime.endEditing(true)
self.textFieldEndTime.text = endtimePickerView.date.endtimestringValue
var a = (textFieldStartTime.text.starttimedateValue)
var b = (textFieldEndTime.text.endtimedateValue)
var difference: String = ""
if let dateA = a, dateB = b {
let difference = dateA.timeIntervalSinceReferenceDate - dateB.timeIntervalSinceReferenceDate
}
textFieldBottomTime.text = "\(difference)"
}

number changed automatically to the string

I am trying to read Excel file through apache poi. In a column I am having the data `"9780547692289". After iterating all the columns, in the output, the number is displaying like this "9.780547692289E12". I mean number changed automatically to the string(because it has'E'). I have to keep this as number only(as it is). What should I do..?
It is probably just a display setting. The "E" is just for scientific notation for displaying the number.
//while getting values from excel u can use this method
private String getCellValue(Cell cell) {
if (cell == null) {
return null;
}
if (cell.getCellType() == Cell.CELL_TYPE_STRING) {
return cell.getStringCellValue();
} else if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
return cell.getNumericCellValue() + "";
} else if (cell.getCellType() == Cell.CELL_TYPE_BOOLEAN) {
return cell.getBooleanCellValue() + "";
}else if(cell.getCellType() == Cell.CELL_TYPE_BLANK){
return cell.getStringCellValue();
}else if(cell.getCellType() == Cell.CELL_TYPE_ERROR){
return cell.getErrorCellValue() + "";
}
else {
return null;
}
}
This worked for me
if (cell.getCellType()==Cell.CELL_TYPE_NUMERIC || cell.getCellType()==Cell.CELL_TYPE_FORMULA )
{
int i = (int)cell.getNumericCellValue();
String cellText = String.valueOf(i);
}