Groovy date format for UTC with milliseconds - date

I'm having trouble finding a good way of formatting a UTC-time stamp with this format: yyyyMMdd-HH:mm:ss.<three additional digits>
I wasn't able to find any character that represents milliseconds/hundredths, I'm not even sure this is possible, to parse that format that is.
Ideally I'd like to use the parseToStringDate that's part of the Date library.
My plan b is to convert yyyyMMdd-HH:mm:ss to milliseconds and then add the three last digits to that number.

Use yyyyMMdd-HH:mm:ss.SSS
This will get you milliseconds as well.
Test Code:
def now = new Date()
println now.format("yyyyMMdd-HH:mm:ss.SSS", TimeZone.getTimeZone('UTC'))

I would convert it like that:
def now = new Date()
println now.format("YYYYMMdd-HH:mm:ss")

You can try this:
TimeZone.getTimeZone('UTC')
Date date = new Date()
String newdate = date.format("YYYY-MM-dd HH:mm:ss.Ms")
log.info newdate

Related

Format a date by reversing the position of the year, month and days

The value received from the Date Picker is in the format "02-06-2020" (formatted with mask = "DD-MM-YYYY" in q-date).
I need to convert it to "2020-06-02" format to send it to the server.
I tried the following, but I get undefined.
Example:
let myDate = "02-06-2020";
console.log(date.formatDate(myDate, "YYYY-MM-DD")) //undefined
I would appreciate suggestions for finding a solution.
Thanks.
You can use moment js.
moment(moment('13-01-2020', 'DD-MM-YYYY')).format('YYYY-MM-DD');
or
date.split("-").reverse().join("-");

Error with DateFormat in dart: "Trying to read -YYYY from 17-04-2020 at position 10"

How to convert a string like '17-04-2020' which is in dd-MM-yyyy format to DateTime object in dart/flutter?
formatter = new DateFormat('dd-MM-YYYY');
formatter.parse(strDate);
I tried the above code and getting an error:
Trying to read -YYYY from 17-04-2020 at position 10
You need lower-case Y's on your date pattern, use this:
"dd-MM-yyyy"
You can check the docs here for available date patterns.
In case you only need two digits of the year, you can also use dd-MM-yy.
var myDate = DateFormat('dd-MM-y').parse('17-04-2020');
intl package

How to convert local date to UTC?

Can anyone help me with the following:
I have am recording date in my UI, which is IST +5:30
First, I want to convert that date to UTC with start time 00:00
Second, I want to convert that to long time (which I think it is
Unix)
Saved to DB
Third, I want to convert a long time back to UTC in format
MM/DD/YYYY.
This is what I tried so far:
const dateUnix => moment(myMomentObj)
.utc()
.format(DATE_TIME_FORMATS.TIME_STAMP);
The above gets a long time which I don't know if it correct.
const dateMoment = moment.unix(dateUnix)
const formatedDate = dateUnix.format('L'); //which should be in MM/DD/YYYY format
But the formatDate is giving me something like 02/12/15235 which is wrong.
Any help is appreciated.
Thanks in advance.
This code might help you
//input in IST +5:30
var inputDate = moment().utcOffset("+05:30").format();
console.log(inputDate);
//moment.unix outputs a Unix timestamp
var unixTs = moment.utc(inputDate).unix();
console.log(unixTs);
//there is a unix method that accepts unix timestamps in seconds followed by format to format it
var formattedDate = moment.unix(unixTs).format("MM/DD/YYYY");
console.log(formattedDate);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>

Converting Date in Extjs

I have a Date string of following format:
'31-OCT-2013'
How do I convert this into Date of following format using Extjs 4:
'08/31/2013'
I am using IE8.
If you have string "31-OCT-2013", you need to:
Convert it into date object
var myDate = Ext.Date.parse("31-OCT-2013", 'd-M-Y');
Format it to as you want
Ext.Date.format(myDate, 'm/d/Y');
Try something like this:
If your day representation would be 2 digit with leading zero, then apply this
var date = Ext.Date.parse("31-OCT-2013", 'd-M-Y');
console.log(Ext.Date.format(date, 'm/d/Y'));
But if your day representation would be without a leading zero, then apply this
var date = Ext.Date.parse("31-OCT-2013", 'j-M-Y');
console.log(Ext.Date.format(date, 'm/d/Y'));
Check the docs for Ext.Date

What is the most efficient way to convert an eight digit number to a date?

I am using ColdFusion 9.0.1 and some database that I cannot change.
I am accessing a database that stores a date as an eight digit numeric with zero decimal places like this:
YYYYMMDD
I need to be able to read the date, add and subtract days from a date, and create new dates. I am looking for a ColdFusion solution to efficiently (not much code) to convert the date to our standard format, which is
MM/DD/YYYY
And then convert it back into the database's format for saving.
I need to code this in such a way that non-ColdFusion programmers can easily read this and use it, copy and modify it for other functions (such as adding a day to a date). So, I am not looking for the most least amount of code, but efficient and readable code.
Can you suggest anything that would make this code block more flexible, readable, or more efficient (less code)?
<cfscript>
// FORMAT DB DATE FOR BROWSER
DateFromDB = "20111116";
DatedToBrowser = createBrowserDate(DateFromDB);
writeOutput(DatedToBrowser);
function createBrowserDate(ThisDate) {
ThisYear = left(ThisDate, 4);
ThisMonth = mid(ThisDate, 4, 2);
ThisDay = right(ThisDate, 2);
NewDate = createDate(ThisYear, ThisMonth, ThisDay);
NewDate = dateFormat(NewDate, "MM/DD/YYYY");
return NewDate;
}
// FORMAT BROWSER DATE FOR DB
DateFromBrowser = "11/16/2011";
DateToDB = createDBDate(DateFromBrowser);
writeDump(DateToDB);
function createDBDate(ThisDate) {
ThisYear = year(ThisDate);
ThisMonth = month(ThisDate);
ThisDay = day(ThisDate);
NewDate = "#ThisYear##ThisMonth##ThisDay#";
return NewDate;
}
</cfscript>
First find who ever did the database and kick them in the nads...
Personally I'd Convert with sql so my code only dealt with date objects.
Select Convert(DateTime, Convert(VarChar(8),DateTimeInventedByIdjitColumn))
From SomeTable
As stated by our peers, store dates as dates.
'08/06/2011' could be 8th of june of the 6th of August depending on locale.
20111643 is a valid integer..
Not using a proper date type is just a massive collection of features and bugs that at best are waiting to happen.
You can actually rewrite each function into 1 line of code.
function createBrowserDate(ThisDate) {
return mid(ThisDate,4,2) & "/" & right(ThisDate,2) & "/" & left(ThisDate,4);
}
and
function createDBDate(ThisDate) {
return dateFormat( ThisDate, "YYYYMMDD" );
}
Don't keep dates as strings - keep dates as dates and format them when you need to.
If you can't correct the database to use actual date columns (which you should if you can), then you can use these two functions to convert to/from YYYYMMDD and a date object:
function parseYMD( YYYYMMDD )
{
if ( ! refind('^\d{8}$' , Arguments.YYYYMMDD ) )
throw "Invalid Format. Expected YYYYMMDD";
return parseDateTime
( Arguments.YYYYMMDD.replaceAll('(?<=^\d{4})|(?=\d{2}$)','-') );
}
function formatYMD( DateObj )
{
return DateFormat( DateObj , 'yyyymmdd' );
}
By using date objects it means that any level of developer can work with them, without needing to care about formatting, via built-in functions like DateAdd, DateCompare, and so on.
I'm not a regular expression fan since it's not that readable to me.
Since you're using CF9, I'd typed the argument and specify the returntype of the functions to be even more readable for the next person picking up your code.
First, right after I read the date from DB, I'd parse it to a Date object using parseDBDate()
Date function parseDBDate(required String dbDate)
{
var yyyy = left(dbDate, 4);
var mm = mid(dbDate, 4, 2);
var dd = right(dbDate, 2);
return createDate(yyyy , mm, dd);
}
Once you have the date object, you can use all those built-in Date functoin like DateAdd() or DateDiff().
Call browserDateFormat() right before you need to display it.
String function browserDateFormat(required Date date)
{
return dateFormat(date, "MM/DD/YYYY");
}
Call dBDateFormat() inside <cfqueryparam value=""> when it's time to persist to DB
String function dBDateFormat(required Date date)
{
return dateFormat(date, "YYYYMMDD");
}
One liner :)
myDateString = "20110203";
myCfDate = createObject("java","java.text.SimpleDateFormat").init("yyyyMMdd").parse(myDateString,createObject("java","java.text.ParsePosition").init(0*0));
If you want to parse different patterns, change "yyyyMMdd" to any other supported pattern.
http://download.oracle.com/javase/1.5.0/docs/api/java/text/SimpleDateFormat.html
The ParsePosition is used to say where to start parsing the string.
0*0 is shorthand for JavaCast("int",0) - in the Adobe cf engine, 0 is a string, until you apply math to it, then it becomes a Double, which the ParsePosition constructor supports. Technically, it constructs with an int, but cf is smart enough to downgrade a Double to an int.
http://download.oracle.com/javase/1.5.0/docs/api/java/text/ParsePosition.html