How to convert local date to UTC? - date

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>

Related

Flutter Convert date time to string failed midnight

I am trying to convert date and time to string with below code but when I convert the below
2020-09-01 00:00:00.000 I get 01-09-2020 24:00 that is wrong
String formattedDate = DateFormat('dd-MM-yyyy kk:mm').format(date);
the correct is 01-09-2020 00:00 how can I get this
Any ideas?
String formattedDate = DateFormat('dd-MM-yyyy kk:mm').format(date);
I have no idea what "kk" is as a format string, but you seem to want "HH" for "hours":
String formattedDate = DateFormat('dd-MM-yyyy HH:mm').format(date);
Please try this, it's work for me :
DateFormat('dd-MM-yyyy hh:mm').format(DateTime.parse("2020-09-01 00:00:00.000"))
The two formats essentially do the same thing but differ in how they
handle midnight. kk will format midnight to 24:00 whereas HH will
format to 00:00. The hours in a day in k are 1-24 and in H are 0-23

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("-");

Weird part of DateTime.parse(data['datetime']) in Dart

Problem Description
I have 020-03-20T14:16:27.189282+08:00 return from my database and I am trying to convert it to 2:16pm using DateTime.parse(). However,every time I convert the time, I get 6:16am.
Question
May I know how could I solve this problem?
MyCode
dateTime = `020-03-20T14:16:27.189282+08:00`
time = DateFormat.jm().format(DateTime.parse(dateTime));
print(time);
Dai, thanks for your small tips.
Solutions for this question is remove the offset from the string.
Why
The result is always in either local time or UTC. If a time zone offset other than UTC is specified, the time is converted to the equivalent UTC time.
Read it here https://api.dart.dev/stable/2.7.1/dart-core/DateTime/parse.html.
Answered
String dateTime = `020-03-20T14:16:27.189282+08:00`;
int indexOfPlusMinusSymbol = dateTime .indexOf('+') >= 0
?dateTime .lastIndexOf('+')
: dateTime .lastIndexOf('-');
String time = DateFormat.jm().format(DateTime.parse(dateTime..substring(0, indexOfPlusMinusSymbol)));
print(time);

Moment Convert From/To ISOString with Timezone

moment = require('moment-timezone')
fromISO = (format, dateISO)->
if !format? then format = 'YYYY-MM-DD HH:mm:ss'
return moment(dateISO).tz("Europe/Amsterdam").format(format)
toISO = (dateTime)->
return moment(dateTime, "DD.MM.YYYY hh:mm:ss").tz("Europe/Amsterdam").toISOString()
Question
First I want to convert from an ISOString to a specific format in a specific timezone.
Second the other way around.
Unfortunately, it ignores the timezone. I always get the same ISOString back. In the browser console, it takes my local time of the browser, on my server it just converts it without subtracting time.
PS: First I used:
return moment(dateISO).utcOffset('+02:00').format(format)
return moment(dateTime, "DD.MM.YYYY hh:mm:ss").utcOffset('-02:00').toISOString()
Currently, I use .add and .subtract
This works in Chrome:
From a local time to ISO:
moment.tz("2014-06-01 12:00", "Europe/Amsterdam").toISOString()
instead of
moment("2014-06-01 12:00").tz("Europe/Amsterdam").toISOString()
From ISO time to local
moment.tz(dateISO,"Europe/Amsterdam").format(format)

Groovy date format for UTC with milliseconds

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