momentjs unix timestamp converts pm to am - javascript

I am having a problem with Moment.js.
Here are the codes.
var date = "2016-07-26 06:15 pm";
var unixDate = moment(date).unix();
var renderDate = moment.unix(unixDate).format("YYYY-MM-DD hh:mm a");
console.log(renderDate);
I want the value of "renderDate" to be same as the value of "date" variable. However, what gets printed out in the console is "2016-07-26 06:15 am".
I am completely lost as to why everything remains the same except "am/pm" at the end.

unix method accepts only number
You need to parse date before usage
moment('2016-07-28 06:15 PM', 'YYYY-MM-DD hh:mm a').format('YYYY/MM/DD hh:mm a')

Look at http://momentjs.com/docs/. You can only use the string without a format if it is in ISO 8601 format. In your example, it ignored the am/pm indicator and viewed the hour as being in 24-hour format.

Related

date-fns/format always takes local timezone

From below code:
const dateString = '1994-09-15T12:00:00-03:00';
const parsedDate = parseISO(dateString)
const dateFormat = 'MM-dd-yyyy HH:mm:ss xxx'
console.log(format(parsedDate, dateFormat, { }))
I expect:
09-15-1994 20:30:00 -03:00 But i get 09-15-1994 20:30:00 +05:30 as my local timezone is +05:30
What am i missing here?
The parseDate() creates a date based on GMT, e.g. is adjusted by 3h based on your input '1994-09-15T12:00:00-03:00'.
The format() function with xxx formats local time without the Z, such as -08:00, +05:30, +00:00. If you want to format with GMT string, specify OOOO, which will give you something like GMT-08:00, GMT+05:30, or GMT+00:00, which again has the offset adjusted based on your browser's time zone.
See Time-Zones docs below if you want to format to a time zone other than your browser's one, such as GMT-03:00.
Docs:
ISO 8601: https://en.wikipedia.org/wiki/ISO_8601
parseDate(): https://date-fns.org/v2.29.2/docs/parseISO
format(): https://date-fns.org/v2.29.2/docs/format
Time-Zones: https://date-fns.org/v2.29.2/docs/Time-Zones

Set CET timezone with toISOString()

How do I change the MYSQL timestamp date to the JS date using toISOString () and setting the time-zone to CET?
This is what I use and it returns the following format "2021-02-251 15:27:20" which is what I want, only the time should be +1 hour "2021-02-251 16:27:20 ":
registration_date_customer.toISOString().replace(/T/, ' ').replace(/\..+/, '')
The time on the database is correct (16:27:20).
Does anyone know how to set the CET time-zone?
You can use Date.localeString() to format in the correct timeZone, the IANA timezone "Europe/Paris" is equivalent to CET. Using the "sv" locale will result in an ISO formatted string (Sweden uses ISO date formatting).
const registration_date_customer = new Date("2021-02-25 15:27:20Z");
console.log(registration_date_customer.toLocaleString("sv", { timeZone: "Europe/Paris"}));

Convert utc time into local time of specifc format

I have a date string of format dd.MM.yyyy HH:mm:ss but its UTC which I want to convert to local. How can I do it using moment library?
const dateStr = '20.09.2018 16:12:37';
const format = 'dd.MM.yyyy HH:mm:ss';
// for local time of UTC +3.00 new date Str will be 20.09.2018 19:12:37
You have to use moment.utc(String, String) to parse your string using UTC mode
By default, moment parses and displays in local time.
If you want to parse or display a moment in UTC, you can use moment.utc() instead of moment()
Then you can use local() to convert it to local time:
Sets a flag on the original moment to use local time to display a moment instead of the original moment's time.
Please note that since you input dateStr is not in ISO 8601/RCF 2822 recognized format you have to specify format when parsing it. Morover moment tokens are case sensitive so you have to use uppercase YYYY instead of yyyy to parse years and uppercase DD to parse day of the month since lowercase dd stands for day of the week (Mon, Tue, etc).
Here a live sample:
const dateStr = '20.09.2018 16:12:37';
const format = 'DD.MM.YYYY HH:mm:ss';
console.log(moment.utc(dateStr, format).local().format(format));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.1/moment.min.js"></script>
Have a look at Local vs UTC vs Offset guide to better understand how UTC and locale mode work in momentjs.
To convert from UTC to Local you need to use moment.local() method
const dateStr = '20.09.2018 16:12:37';
const format = 'DD.MM.YYYY HH:mm:ss';
var newDate = moment.utc().format(format);
var temp= moment.utc(newDate, format);
var local = moment(temp).local().format(format);
More info on the official Documentation

convert local date time to UTC as yy:mm:dd H:M format

I need to convert local date to UTC or ISO date fromat as yy:mm:dd H:M or date diff locale date time with 03:30 as yy:mm:dd H:M
2016-10-22T04:30:00.000Z
convert to
2016-10-22T01:00:00.000Z
I'm not sure I fully understand your question but if you need to subtract 3:30 from a Date object and display it in ISO format then something like this should work.
(you don't need datejs.com)
function subtractTime(dateObj, hours, mins) {
dateObj.setHours(dateObj.getHours() - hours);
dateObj.setMinutes(dateObj.getMinutes() - mins);
}
var date = new Date('2016-10-22T04:30:00.000Z');
subtractTime(date, 3, 30);
console.log(date.toISOString());
Please check out datejs: http://www.datejs.com/. Then you can do,
Date.parse('2016-10-22T04:30:00.000Z').addHours(-3).addMinutes(-30). toISOString()
// 2016-10-22T04:30:00.000Z

How to convert a given date/time for a specific timezone (not local) to UTC using moment.js and moment-timezone.js

How can I I want to convert a given date/time for a specific timezone (not local) to UTC using moment.js and moment-timezone.js
I use:
var s = moment("10/15/2014 09:25 AM").tz("America/Los_Angeles").format('hh:mm:ss a');
I have a difficulty when I want to give a value on moment().
Let me explain you the facts:
I have to take a date/time value from a cell with the following format:10/15/2014 09:25 AM (MM/DD/YYYY h:mm a). This value is not a constant one, its the opened time for some entries.
I want to transform this string in UTC. Unfortunately, the string is not my local time, is in America/Los_Angeles (PDT/PST) timezone. I want also to take care automatically about PDT(9 Mar, 2 Nov) and PST.
-10/15/2014 09:25 AM America/Los_Angeles -07:00 => 10/15/2014 04:25 PM UTC 00:00
-12/15/2014 09:25 AM America/Los_Angeles -08:00 => 12/15/2014 05:25 PM UTC 00:00
How can I do this?
If I use: Var s = moment("10/15/2014 09:37 PM").tz("America/Los_Angeles").format('hh:mm:ss a');
...it will be parsed as my local time and it will be converted to America/Los_Angeles.
- 10/15/2014 09:25 AM Eastern European Time +03:00 => 10/15/2014 11:25 PM America/Los_Angeles +07:00
I think that a short description for my problem is this:
- How can I "tell" to script that this string 10/15/2014 09:37 AM is from a specific timezone. After this, the conversion to UTC is piece of cake.
Thanks.
You must provide a pattern when you want to parse a date string with a specific timezone, except for UTC. For UTC you can just provide the date string.
Usage: moment.tz(string, pattern, zoneString)
In your case:
var moment = require("moment-timezone");
var d1 = moment.tz("10/15/2014 09:25 AM", "MM/DD//YYYY hh:mm A", "America/Los_Angeles");
d1.toString(); // Wed Oct 15 2014 09:25:00 GMT-0700
d1.tz("UTC").format('hh:mm:ss a'); // '04:25:00 pm'
If you omit the formatter the parsing is wrong, but there is a deprecated note: https://github.com/moment/moment/issues/1407
Read this http://momentjs.com/timezone/docs/#/using-timezones/parsing-ambiguous-inputs/ to handle DST

Categories