I created some funtion but how to convert string timestamp to date format for the given variabe.
var data="/Date(1424853425207)/";
data=data.parse(data);
consoe.log(data);
But enabe show the date for the above variable
You can fetch the timestamp using regex and then cast to a number so it can be used to create a new instance of Date:
var data = "/Date(1424853425207)/";
var timestamp = +data.replace(/\/Date\((.*?)\)\//g, '$1');
var date = new Date(timestamp);
console.log(date); // Wed Feb 25 2015 09:37:05 GMT+0100
Related
I'm assign a new date object to my object attribute like that :
giftObject.purshasedDate = new Date()
which give a date format :
Date Thu Feb 20 2020 13:36:37 GMT+0100 (heure normale d’Europe
centrale)
I want to increase this date by one year, I tried :
new Date().setFullYear(giftObject.purshasedDate.getFullYear() + 1) but it give a number serial like this : 1613824899244
I do not understand what that number serial mean! it's a date or should a try some thing else ?
By default all dates object are timestamps.
JavaScript Date objects represent a single moment in time in a
platform-independent format. Date objects contain a Number that
represents milliseconds since 1 January 1970 UTC.
Source : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date
I think the default new Date() object can display itself to string by in fact it's also a timestamp.
If you want to display a date as string, you have to use the toLocaleString() method on Date.
I tried by updating the original date and it return the string of the date, don't know why but it's work by updating the original date.
Example :
let giftObject = {};
giftObject.purshasedDate = new Date();
giftObject.purshasedDate.setFullYear(giftObject.purshasedDate.getFullYear() + 1);
console.log(giftObject.purshasedDate)
Result : "20/02/2021 à 13:55:49" for my French browser
const oldDate = new Date("Date Thu Feb 20 2020 13:36:37 GMT+0100")
const newDate = oldDate.setFullYear(oldDate.getFullYear() + 1)
const dateWithPlusOneYear = new Date(newDate)
console.log(new Date(dateWithPlusOneYear))
//Sat Feb 20 2021 13:36:37 GMT+0100 (Central European Standard Time)
Please use this one:
purshasedDate = new Date();
purshasedDate = new Date(purshasedDate.setFullYear(purshasedDate.getFullYear() + 1));
I am receiving a form value for date in Kendo Datepicker. It comes in as an object.
1)
How do I convert the following to date?
The following in Debugger still left it as Object
new Date(this.editHeaderAddressForm.value.seasonalEnd);
2) Additionally, how can I convert it to Moment?
The Date.parse() method parses a string representation of a date, and returns the number of milliseconds
var javaScriptRelease = Date.parse('04 Dec 1995 00:12:00 GMT');
var newDate = Date('December 17, 1995 03:24:00');
console.log(javaScriptRelease);
> // expected output: 818035920000
console.log(newDate);
> // expected output: Sun Dec 17 1995 03:24:00 GMT...
You can get date, and moment with short and simple code.
var full_time = new Date(this.editHeaderAddressForm.value.seasonalEnd);
var date = full_time.toLocaleDateString();
var moment = full_time.toLocaleTimeString();
console.log(date);
console.log(moment);
Use Date.parse method
const epochValue = Date.parse(this.editHeaderAddressForm.value.seasonalEnd);
This will return date in EPOCH this you can directly pass in to Moment function as well.
moment.unix(epochValue)
In my html file I'm passing an array of objects with a date time to javascript like this
<script>
var TimerEvents = #Html.Raw(Json.Encode(Model.PendingTimerEvents));
</script>
my 'TimerEvents' has an EventTime property that when I read in Javascript looks like this
"/Date(1521617700000)/"
and I want to get this value, whatever it is, into a Javascript Date() object.
like this
var countDownDate = new Date(date here).getTime();
What is that format, and how would I do this?
the value you get is a milisecond representation of the date, I think the count starts at 1/1/1970.
Anyway here is a solution for formatting it:
Converting milliseconds to a date (jQuery/JavaScript)
Assuming that Model.PendingTimerEvents is a DateTime, you could just do:
var TimerDate = new Date(#Model.PendingTimerEvents.Year, #Model.PendingTimerEvents.Month, #Model.PendingTimerEvents.Day, #Model.PendingTimerEvents.Hour, #Model.PendingTimerEvents.Minute, #Model.PendingTimerEvents.Second);
You can extract the date with regex if you need to:
<script>
var TimerEvents = #Html.Raw(Json.Encode(Model.PendingTimerEvents)); //"/Date(1521617700000)/"
var dateString = TimerEvents.match(/\d+/)[0] // "1521617700000"
var date = new Date(+dateString); //convert to number
console.log(date); // Wed Mar 21 2018 08:35:00 GMT+0100 (Mitteleuropäische Zeit)
</script>
I need to display a formatted date from a timestamp provided by Google Analytics
Standard solution like
var date = new Date(timestamp * 1000);
var formatted = date.toString();
produces wrong value Jan 01 1970. That's because of timestamp format.
In PHP I can specify the timestamp format:
\DateTime::createFromFormat('Ymd', $timestamp);
How to do this in JS?
Since the dates you are receiving are formatted as YYYYMMDD, not as a Unix
timestamp, you can parse it by
extracting the year, month and date using String.prototype.slice.
var timestamp = '20170306',
year = parseInt(timestamp.slice(0, 4), 10),
month = parseInt(timestamp.slice(5, 6), 10),
day = parseInt(timestamp.slice(7, 8), 10);
// - 1 because the Date constructor expects a 0-based month
date = new Date(Date.UTC(year, month - 1, day)),
gmt = date.toGMTString(),
local = date.toString();
console.log('GMT:', gmt); // Mon, 06 Mar 2017 00:00:00 GMT
console.log('Local:', local);
This assumes that the dates you are using are in UTC (which they likely are). Date.UTC creates a timestamp (in milliseconds since Unix epoch) and then feeds it into new Date() which uses it to create a Date object representing that time. .toGMTString() outputs the date formatted for the GMT timezone. To output it formatted in local time, use .toString() instead.
try this type:
var userDate = new Date();
var day = userDate.getDate();
var month = userDate.getMonth() + 1;
var year = userDate.getFullYear();
alert("Date Formate is :"+year+"-"+month + "-" + day);
In javascript you can use an external library like moment.js
var date = moment.unix(timestamp);
date.format("YYYY MM DD");
See detail about .format here https://momentjs.com/docs/#/displaying/format/
I have my time since epoch stored as a number: 1444749469000. However, when I try to convert it to a Date object, using Date(1444749469000), it just gives me the current date instead of the one it should be (around Tue Oct 13 2015).
> Date(1444749469000)
"Tue Apr 12 2016 09:28:30 GMT-0700 (PDT)"
You need a new before the Date because Date is a constructor:
var d = new Date(1444749469000)
alert(d);
Because when you call Date as a function, it will return a string of current date and ignore the given value. In order to retrieve the Date object, you must initialize the Date constructor with keyword new.
var now = Date(1444749469000);
var date = new Date(1444749469000);
console.log(typeof now); //string
console.log(typeof date); //object