I am using this following way while doing an export to excel
alasql.fn.Date = Date;
alasql('SELECT new Date(mydateString) AS CUSTOM_DATE INTO XLS("' + filename + '.xls",{headers:true}) FROM ?', [items]);
Those above lines are printing in this below date format
Sat Jun 06 2015 00:00:00 GMT-0500 (Central Daylight Time)
but I want to format mydateString to this below format
MM-DD-YYYY hh:mm:ss
How can I do that?
I did this below instead and create my own format for date
alasql.fn.datetime = function(dateStr) {
var date = new Date(dateStr);
return date.toLocaleString();
};
alasql('SELECT datetime(mydateString) AS CUSTOM_DATE INTO XLS("' + filename + '.xls",{headers:true}) FROM ?', [items]);
Related
I get from API a json object with datetime field in format:
"date": "6/23/2018 12:00:00 AM",
How to transform it to valid date time object in Dart?
While parsing it I get an error
Invalid date format
class User {
int id;
DateTime date;
User({this.id,this.date});
factory User.fromJson(Map<String, dynamic> json) {
return User(
id: json['id'],
date: DateTime.parse(json['date'])
);
}
}
final dateRegExp =
RegExp(r'(\d{1,2})/(\d{1,2})/(\d{4}) (\d{2}):(\d{2}):(\d{2}) (AM|PM)');
var match = dateRegExp.firstMatch('6/23/2018 12:00:00 AM');
var result = DateTime(
int.parse(match[3]),
int.parse(match[1]),
int.parse(match[2]),
int.parse(match[4]) + (match[7] == 'AM' ? 0 : 12),
int.parse(match[5]),
int.parse(match[6]),
);
print(result);
You can do this in a simpler way by using this package, Jiffy. It tries to mimic the simplicity of momentjs
To get the DateTime object
DateTime d = Jiffy("6/23/2018 12:00:00 AM", "MM/dd/yyyy hh:mm:ss a").dateTime;
You can also format it
Jiffy("6/23/2018 12:00:00 AM", "MM/dd/yyyy hh:mm:ss a").format("yyyy, MM dd"); // 2018, 06 23
// You can also use default formats
Jiffy("6/23/2018 12:00:00 AM", "MM/dd/yyyy hh:mm:ss a").yMMMEdjm; // Sat, Jun 23, 2018 12:00 AM
I'm getting a string representation of a date from a json that looks like the following:
let dateString = "2016-12-31T00:10:00+01:00"
In order to model it as a Date object I'm using a date formatter like so:
let dateForm = DateFormatter()
dateForm.locale = Locale(identifier: "fr_FR")
dateForm.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZZZZZ"
dateForm.timeZone = TimeZone.current
When I turn it into a Date, my Playground output is correct:
let date = dateForm.date(from: dateString)
=> Output: "Dec 31, 2016, 12:10 AM"
But if I try to print this exact same object (date) I get the following output:
print(date!)
=> Output: "2016-12-30 23:10:00 +0000\n"
My question is: How can I be sure that I'm dealing with the correct date (by correct I mean with my local time zone (GMT+01)) ?
When you print the date output 2016-12-30 23:10:00 +0000 and your GMT is +00:00
but when you get the string from date it will return string as per your given format And your Locale (fr_FR) output 2016-12-31T00:10:00+01:00 and your GMT is +01:00
if you want to date from string then
date output = your string date - (your GMT)
In your case
2016-12-30 23:10:00 = 2016-12-31 00:10:00 - (+01:00)
I am fairly new to Groovy (but already loving it). I am not new to coding but haven't had much experience so far.
What am I doing?
I am extracting certain information from an excel file to create a XML (SOAP) message from it to forward it to a web-service. Everything works fine so far except the Date conversion.
I am saving the string date to a var
odate = 'Wed Oct 31 00:00:00 CET 2012'
I need to reformat this Date into something like
"10/31/2012 10:09:00" (MM/dd/yyyy HH:mm:ss)
I have tried to parse the date as mentioned in another question but all I get is an exception.
String odate = 'Wed Oct 31 00:00:00 CET 2012'
def parsedodate = new Date().parse('E MMM dd H:m:s z yyyy', odate)
println parsedodate.format('MM/dd/yyyy h:m:s')
Exception thrown
31.10.2012 10:18:25 org.codehaus.groovy.runtime.StackTraceUtils sanitize
WARNUNG: Sanitizing stacktrace:
java.text.ParseException: Unparseable date: "Wed Oct 31 00:00:00 CET 2012"
Now after a little reading and some rounds of trial & error I found out that somehow the parse method seems to only interpret german dates. The following works after manually changing the string date to a german format (which is whre I am located).
String odate = 'Mi Okt 31 00:00:00 2012' //Mi = Wednesday, Okt = October, removed timezone
def parsedodate = new Date().parse('E MMM dd H:m:s yyyy', odate) // removed the z
println parsedodate .format('MM/dd/yyyy h:m:s')
However, I need the parser to accept the english date format.
What do I do (wrong)?
Whole groovy solution for your problem would be:
import java.text.SimpleDateFormat
odate="Wed Oct 31 00:00:00 CET 2012"
englishPattern="E MMM dd H:m:s z yyyy"
SimpleDateFormat englishDateFormat = new SimpleDateFormat( englishPattern , Locale.ENGLISH);
//SimpleDateFormat germanDateFormat = new SimpleDateFormat( germanPattern , Locale.GERMAN);
Date englishDate = englishDateFormat.parse( odate );
//Date germanDate = germanDateFormat.parse( odate );
String englishOutput = englishDate .format( englishPattern );
//String germanOutput = germanDate .format( germanPattern );
englishDate.format("MM/dd/yyyy hh:mm:ss")
You will need to use some Java to access Locale aware SimpleDateFormat instance.
SimpleDateFormat englishDateFormat = new SimpleDateFormat( englishPattern , Locale.ENGLISH);
SimpleDateFormat germanDateFormat = new SimpleDateFormat( germanPattern , Locale.GERMAN);
Date englishDate = englishDateFormat.parse( odate );
Date germanDate = germanDateFormat.parse( odate );
String englishOutput = englishDate .format( englishPattern );
String germanOutput = germanDate .format( germanPattern );
I have two strings:
String date = "2011-11-11"
String time="11:00 PM"
i want to merge this date and time and convert them into a long, similar to System.currentTimeMillis().
try this it is working fine
String inputDate=date+" "+time ;;
long parsedDate = HttpDateParser.parse(inputDate);//inputDate should 2011-12-11 11:10:00 PM" formate
System.out.println("========================="+parsedDate);
Date date=new Date(parsedDate);
SimpleDateFormat date1=new SimpleDateFormat("yyyy-mm-dd hh:mm aa");
String opdate=date1.format(date);
System.out.println("========================="+opdate);
Use SimpleDateFormat and parse the String into a Date.
When you have Date you can get .getTime() what's a long
http://download.oracle.com/javase/1.4.2/docs/api/java/text/SimpleDateFormat.html
NOT TESTED!!
String date = "2011-11-11";
String time = "11:00 PM";
String toParse = date + " " + time;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd hh:mm aa");
try {
Date parse = sdf.parse(toParse);
parse.getTime();
} catch (ParseException ex) {
}
String dateTime = "2011-11-11 " + time;
DateFormat formatter ;
Date date ;
formatter = new SimpleDateFormat("dd-MMM-yy HH:MM");
date = (Date)formatter.parse(dateTime );
long time = date.getTime();
I found this SO post in the same lines.
String date =date+time ;
SimpleDateFormat fmt = new SimpleDateFormat("MM-dd-yyyy HH:mm");
Date myDate = fmt.parse(date);
System.out.println(myDate); //Mon Jun 04 07:05:00 EDT 2007
long timestamp = myDate.getTime();
I having a problem when convert String to Date in Blackberry SDK. Please support for me.
This is my code:
String date = "Mon May 09 09:00:00 GMT 2011";
Date formatter = new Date();
formatter.setTime(HttpDateParser.parse(date));
SimpleDateFormat dateFormat = new SimpleDateFormat(formatStr);
string dateString = dateFormat.format(formatter);
purpsoe of the function is format date MMM dd, YYYY.
But after i run the function, it will return result that I unexpected.
Expected: May 09, 2011
UnExpected : Jan,01,1970.
I suspect the "Mon May 09 09:00:00 GMT 2011" is not a supported by HttpDateParser date format. Since the HttpDateParser.parse() does not throw an exception, I guess it simply return 0 in case of an unsupported format.