I'm using the module dataformat to get a data from a timestamp but I get an error and I can't understand why.
that's the timestamp 1405303200 and I need the Date in this format dd/mm/yyyy
That's my code
var day=dateFormat(result.request_date, "dd/mm/yyyy");
But what I get is the 1970-01-17 date asd.
I think you are using a timestamp saved by PHP... now convert it to miliseconds
var timestamp = 1405303200 *1000;
Related
I am saving the date to SQL database from nodejs. While saving the date to SQL I am getting error " parameter.value.getTime ".
While passing a date in this format while passing it to SQL.
2018-06-27T18:30:00.000Z
In early records in the database, DateTime format is this one.
2018-05-25 14:39:19.433
How can I solve this error? I already used moment js to use another format for saving the date but it is not working.
You should not try to change the format of your String, but convert it into JavaScript Date instead:
var yourDate = new Date(yourString);
I'm trying to convert foreign time to Local Time. I'm getting a date and time in Europe/London. Currently I'm using moment-timezone to get my code working, however its giving me a wrong output.
resultDate = new moment('2017-06-30T22:10:00').tz('Europe/London').format('YYYY-MM-DD HH:mm:ss');
I think the code thinks that the date input is already in local time where I need to convert it into Europe/London which would give a local result, where as what I want is to actually convert the foreign time to local time.
In short the date and time as my input (2017-06-30T22:00:00), I am expecting it to be 7 hours in advance (2017-07-1T05:00:00) since I currently live in Asia/Manila, 7 hours in advance to London. However I'm getting 2017/06/30 15:00:00 +0100 as my result.
Is there a way for me to do this by utilizing the information 'Europe/London' or 'Asia/Manila' as seen in my code?
You can use moment.tz to parse your input as Europe/London time and then use the tz function to convert it to Asia/Manila.
The first parses your input using the given timezone while the latter convert a moment objet to a given timezone.
Here a working sample:
// Parse input considering as London tz
var timeInLondon = moment.tz('2017-06-30T22:10:00', 'Europe/London');
// Converting input to Manila
var timeInManila = timeInLondon.tz('Asia/Manila');
// Show result
console.log(timeInManila.format('YYYY-MM-DD HH:mm:ss'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.11/moment-timezone-with-data-2010-2020.min.js"></script>
I am trying to get the date time in moment js in this format :
2016-12-19T09:43:45.672Z
The problem is I am able to get the time format as
2016-12-19T15:04:09+05:30
using
moment().format();
but I need the format as the first one [like .672Z instead of +05:30]. Any suggestions would be of great help.
From the documentation on the format method:
To escape characters in format strings, you can wrap the characters in square brackets.
Since "Z" is a format token for the timezone, you need to escape it. So the format string you are after is:
moment().format('YYYY-MM-DD[T]HH:mm:ss.SSS[Z]');
As #VincenzoC said in a comment, the toISOString() method would also give you the format you are after.
Use moment.utc() to display time in UTC time instead of local time:
var dateValue = moment().utc().format('YYYY-MM-DDTHH:mm:ss') + 'Z';
or moment().toISOString() to display a string in ISO format (format: YYYY-MM-DDTHH:mm:ss.sssZ, the timezone is always UTC):
var dateValue = moment().toISOString();
Try this
const start_date = '2018-09-30';
const t = moment(start_date).utc().format();
console.log(t);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>
var dateTime = new Date("2015-06-17 14:24:36");
dateTime = moment(dateTime).format("YYYY-MM-DD HH:mm:ss");
Try this. You should have the date in the correct format.
I am trying to extract the unix timestamp (epoch) from a date string as follows:
var week = $("#week").val();
var timestamp = moment(week).format("X");
console.log(timestamp);
This returns, in the console "Invalid Date".
I am passing the following format: "03-Nov-16"
I am trying to return the unix timestamp of a date.
Any help on this would be appreciated.
You need to tell moment the format of the date string you're trying to parse. I think you're looking for something like this (not tested):
var timestamp = moment("03-Nov-16", "DD-MMM-YY").unix()
See the official docs on date string parsing for more info.
//In Node/JS
myDate = moment(data.myTime.format('YYYY/MM/DD HH:MM:SS')).toISOString();
//myDate shows '2014-09-24T04:09:00.000Z'
Insert INTO (dateColumn..) Values(myDate)...
This is the error I get after inserting, note column in Mysql is a "datetime" type.
MySQL Error:: { [Error: ER_TRUNCATED_WRONG_VALUE: Incorrect datetime value: '2014-09- 24T04:09:00.000Z' for column '_dateColumn' at row 1]
code: 'ER_TRUNCATED_WRONG_VALUE',
This result happens because you are using the toISOString() method and it is not a valid format to insert into your DATETIME column. The correct format probably is YYYY-MM-DD HH:MM:SS(I think it depends on MySQL configuration, but this is the default) as the docs points out.
So you should try using moment's format() method like this:
myDate = moment(data.myTime.format('YYYY/MM/DD HH:mm:ss')).format("YYYY-MM-DD HH:mm:ss");
In fact, I don't know what data.myTime is, but if its a moment object too, you can change the first format() method and remove the second one.
DontVoteMeDown answer is right, except that the minutes 'mm' and seconds 'ss' need to be in lowercase, otherwise a wrong value is returned:
myDate = moment(new Date()).format("YYYY-MM-DD HH:mm:ss");
Also, you should check the value sent by javascript before to do your SQL query, in PHP:
DateTime::createFromFormat('Y-m-d H:i:s', $myDate);
Will return false if the date is misformatted.
Here's a function extension to format it into the MySQL DateTime format
moment.prototype.toMySqlDateTime = function () {
return this.format('YYYY-MM-DD HH:mm:ss');
};
You'll be able to do: moment().toMySqlDateTime();
(I'm working with Node.js, Express and MySql and this has worked for me:)
var momentDate=new moment('2018-08-31T20:13:00.000Z');
var readyToInsert=momentDate.format("YYYY-MM-DD HH:mm:ss");
To generate mysql format datetime you can use this:
var moment = require('moment');
let currentTimestamp = moment().unix();//in seconds
let currentDatetime = moment(currentTimestamp*1000).format("YYYY-MM-DD HH:mm:ss");//mysql datetime.
currentTimestamp = moment(currentDatetime).valueOf();//current timestamp in milliseconds
moment().format("YYYY-MM-DD HH:mm:ss")
I think this is the correct format. Otherwise you get the months inserted in your minutes, and you also get seconds above 60.
myDate = moment(data.myTime.format('YYYY/MM/DD HH:MM:ss')).format("YYYY-MM-DD HH:MM:SS");
Tested in Chrome with Moment.js 2.9.0