Pertemuan Ke-enaaaaaaaaaaammmmmm~
Seperti jumat-jumat lain nya ]yang selalu ada keyword kalau diabsen….. And keyword minggu ini adalah “Siapakah dosen yang kamu kagumi selama kuliah di sini” kecuali dosen yang ngajar mata kuliah ini. Pas nama aku dipanggil dengan lantang aku jawab “Bu Dini Arianti” soalnya memang Bu Dini Arianti adalah salah satu dosen favorit aku….. Selain beliau supel and gaul, beliau kalau ngajar tuh asik banget… nerap pisan atuhlah kalau belai yang ngajar… entah kenapa ehehehe
Oke, kita lanjut….. Materi di pertemuan sekarang yaitu “Play With Date and Time” video tutorial seperti biasanya….
Date and time sangat berpengaruh apalagi dalam informasi informasi ,si pembaca bisa mengetahui informasi kapan dip eng update missal kan empunya sosmed meng update status nya.
Dalam c# pembuatan date and time sangat mudah
Baik kita Lanjut saja ke praktikum:
Seperti biasa sediakan semua alat untuk membuat sebuah project yang berupa
MS.visual studio 2010 express for windows phone
• File- new project
• installed templates –visual c# – windows phone application
Name : dateandtime
Location : c:\users\administrator\documents\visual studio 2010\Projects
Solution name : dateandtime
Check list :create directory for solution
Ok
• select the windows phone platform : windows phone os 7.1 – ok
o jika anda manggunakan software Ms.windows phone versi lain anda dengan platform yang lebih tinggi ,maka disaran kan untuk anda agar tetap memilih windows phone os 7.1 karena o.s 8.0 belum support dengan os yang lama
• pada bagian MainPage.xaml
§ di bagian desain view yang di butuh kan :button ,textblock
pada bagian properties ganti :
ü button :
Name : showtimebt
Content : Show Time
ü text block :
Name :timetextblock
Text : -
TextWrapping : wrap
• double klik pada button Click Me dan masuk pada MainPage.xaml.cs (pada bagian script)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
namespace dateandtime
{
public partial class MainPage : PhoneApplicationPage
{
// Constructor
public MainPage()
{
InitializeComponent();
}
private void showtimebt_Click(object sender, RoutedEventArgs e)
{
DateTime mytime = DateTime.Now;
timetextBlock.Text = mytime.ToString();
}
}
}
• f5
• input output : jika di klik pada button maka akan tampil pada text block berupa
10/28/2013 4:29:46 PM
• membuat tanda pemisah antara tanggal dan waktu
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
namespace dateandtime
{
public partial class MainPage : PhoneApplicationPage
{
// Constructor
public MainPage()
{
InitializeComponent();
}
private void showtimebt_Click(object sender, RoutedEventArgs e)
{
DateTime mytime = DateTime.Now;
//timetextBlock.Text = mytime.ToString();
timetextBlock.Text = string.Format(“Date {0} ,Time {1}”, mytime.ToShortDateString(), mytime.ToShortTimeString());
}
}
}
• f5
• output
click button show time : Date 10/28/2013 ,Time 4:41 PM
• membuat lebih panjang lagi
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
namespace dateandtime
{
public partial class MainPage : PhoneApplicationPage
{
// Constructor
public MainPage()
{
InitializeComponent();
}
private void showtimebt_Click(object sender, RoutedEventArgs e)
{
DateTime mytime = DateTime.Now;
//timetextBlock.Text = mytime.ToString();
// timetextBlock.Text = string.Format(“Date {0} ,Time {1}”, mytime.ToShortDateString(), mytime.ToShortTimeString());
timetextBlock.Text = string.Format(“Date {0} ,time {1}”, mytime.ToLongDateString(), mytime.ToLongTimeString());
}
}
}
• f5
• output
clik button show time ate Monday , October 28,2013 , Time 4:47:38 PM
• Ada beberapa fungsi lain juga yang bisa di terap kan misalnya:tanggal ke 5 dari tanggal sekarang
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
namespace dateandtime
{
public partial class MainPage : PhoneApplicationPage
{
// Constructor
public MainPage()
{
InitializeComponent();
}
private void showtimebt_Click(object sender, RoutedEventArgs e)
{
DateTime mytime = DateTime.Now;
//timetextBlock.Text = mytime.ToString();
// timetextBlock.Text = string.Format(“Date {0} ,Time {1}”, mytime.ToShortDateString(), mytime.ToShortTimeString());
//timetextBlock.Text = string.Format(“Date {0} ,time {1}”, mytime.ToLongDateString(), mytime.ToLongTimeString());
timetextBlock.Text = DateTime.Now.AddDays(5).ToString();
}
}
}
• F5
• Output
Clik show time : 11/2/2013 , 4:53:24 PM
• Pengurangan tanggal atau 5 hari tanggal sebelum nya tinggal tuliskan:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
namespace dateandtime
{
public partial class MainPage : PhoneApplicationPage
{
// Constructor
public MainPage()
{
InitializeComponent();
}
private void showtimebt_Click(object sender, RoutedEventArgs e)
{
DateTime mytime = DateTime.Now;
//timetextBlock.Text = mytime.ToString();
// timetextBlock.Text = string.Format(“Date {0} ,Time {1}”, mytime.ToShortDateString(), mytime.ToShortTimeString());
//timetextBlock.Text = string.Format(“Date {0} ,time {1}”, mytime.ToLongDateString(), mytime.ToLongTimeString());
// timetextBlock.Text = DateTime.Now.AddDays(5).ToString();
timetextBlock.Text = DateTime.Now.AddDays(-5).ToString();
}
}
}
• F5
• Ini berlaku untuk penghitungan ,bulan,tahun,hari ,setik,jam,menit dsb
• Untuk memunculkan bulan atau tahun sekarang
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
namespace dateandtime
{
public partial class MainPage : PhoneApplicationPage
{
// Constructor
public MainPage()
{
InitializeComponent();
}
private void showtimebt_Click(object sender, RoutedEventArgs e)
{
DateTime mytime = DateTime.Now;
//timetextBlock.Text = mytime.ToString();
// timetextBlock.Text = string.Format(“Date {0} ,Time {1}”, mytime.ToShortDateString(), mytime.ToShortTimeString());
//timetextBlock.Text = string.Format(“Date {0} ,time {1}”, mytime.ToLongDateString(), mytime.ToLongTimeString());
// timetextBlock.Text = DateTime.Now.AddDays(5).ToString();
//timetextBlock.Text = DateTime.Now.AddDays(-5).ToString();
timetextBlock.Text = DateTime.Now.Month.ToString();
}
}
}
• F5
• Output :10
• Merubah tanggal dulu baru bulan
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
namespace dateandtime
{
public partial class MainPage : PhoneApplicationPage
{
// Constructor
public MainPage()
{
InitializeComponent();
}
private void showtimebt_Click(object sender, RoutedEventArgs e)
{
DateTime mytime = DateTime.Now;
//timetextBlock.Text = mytime.ToString();
// timetextBlock.Text = string.Format(“Date {0} ,Time {1}”, mytime.ToShortDateString(), mytime.ToShortTimeString());
//timetextBlock.Text = string.Format(“Date {0} ,time {1}”, mytime.ToLongDateString(), mytime.ToLongTimeString());
// timetextBlock.Text = DateTime.Now.AddDays(5).ToString();
//timetextBlock.Text = DateTime.Now.AddDays(-5).ToString();
// timetextBlock.Text = DateTime.Now.Month.ToString();
timetextBlock.Text = mytime.ToString(“dd MMMM yyyy hh:mm:ss”);
}
}
}
• F5
• Output :
28 october 2013 05:05: 34
• Mengubah kedalam format Indonesia
Untuk menggunakan cultureinfo harus menggunakan using System.Globalization;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Globalization;
namespace dateandtime
{
public partial class MainPage : PhoneApplicationPage
{
// Constructor
public MainPage()
{
InitializeComponent();
}
private void showtimebt_Click(object sender, RoutedEventArgs e)
{
DateTime mytime = DateTime.Now;
//timetextBlock.Text = mytime.ToString();
// timetextBlock.Text = string.Format(“Date {0} ,Time {1}”, mytime.ToShortDateString(), mytime.ToShortTimeString());
//timetextBlock.Text = string.Format(“Date {0} ,time {1}”, mytime.ToLongDateString(), mytime.ToLongTimeString());
// timetextBlock.Text = DateTime.Now.AddDays(5).ToString();
//timetextBlock.Text = DateTime.Now.AddDays(-5).ToString();
// timetextBlock.Text = DateTime.Now.Month.ToString();
CultureInfo cultureInfo = new CultureInfo(“id-Id”);
timetextBlock.Text = mytime.ToString(“dd MMMM yyyy hh:mm:ss”,cultureInfo );
}
}
}
• F5
• Output:
28 oktober 2013 05 :12:45
• Untuk mendapatkan kode kode seperti {dd,mm,yy} anda bisa kunjungi ke
http://msdn.microsoft.com/en.uc/library/8kb3ddd4.aspx
• Penghitungan jam
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Globalization;
namespace dateandtime
{
public partial class MainPage : PhoneApplicationPage
{
// Constructor
public MainPage()
{
InitializeComponent();
}
private void showtimebt_Click(object sender, RoutedEventArgs e)
{
//DateTime mytime = DateTime.Now;
//timetextBlock.Text = mytime.ToString();
// timetextBlock.Text = string.Format(“Date {0} ,Time {1}”, mytime.ToShortDateString(), mytime.ToShortTimeString());
//timetextBlock.Text = string.Format(“Date {0} ,time {1}”, mytime.ToLongDateString(), mytime.ToLongTimeString());
// timetextBlock.Text = DateTime.Now.AddDays(5).ToString();
//timetextBlock.Text = DateTime.Now.AddDays(-5).ToString();
// timetextBlock.Text = DateTime.Now.Month.ToString();
// CultureInfo cultureInfo = new CultureInfo(“id-Id”);
//timetextBlock.Text = mytime.ToString(“dd MMMM yyyy hh:mm:ss”,cultureInfo );
DateTime myBirtday = new DateTime(1988, 01, 30);
TimeSpan timespan = DateTime.Now.Subtract(myBirtday);
timetextBlock.Text = string.Format(“{0} hari{1} jam{2} detik”, timespan.Days, timespan.Hours, timespan.Seconds);
}
}
}
• F5
• Output:akan keluar perhitungan tahun sekarang dikurang tahun kelahiran dan akan memberitahukan tentang perhitungan waktu waktu nya
• Merubah ke time minunts ago dan ago ago lain nya
• Membuat method dan mengembalikan nilai string
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Globalization;
namespace dateandtime
{
public partial class MainPage : PhoneApplicationPage
{
// Constructor
public MainPage()
{
InitializeComponent();
}
private void showtimebt_Click(object sender, RoutedEventArgs e)
{
//DateTime mytime = DateTime.Now;
//timetextBlock.Text = mytime.ToString();
// timetextBlock.Text = string.Format(“Date {0} ,Time {1}”, mytime.ToShortDateString(), mytime.ToShortTimeString());
//timetextBlock.Text = string.Format(“Date {0} ,time {1}”, mytime.ToLongDateString(), mytime.ToLongTimeString());
// timetextBlock.Text = DateTime.Now.AddDays(5).ToString();
//timetextBlock.Text = DateTime.Now.AddDays(-5).ToString();
// timetextBlock.Text = DateTime.Now.Month.ToString();
// CultureInfo cultureInfo = new CultureInfo(“id-Id”);
//timetextBlock.Text = mytime.ToString(“dd MMMM yyyy hh:mm:ss”,cultureInfo );
DateTime myBirtday = new DateTime(1988, 01, 30);
TimeSpan timespan = DateTime.Now.Subtract(myBirtday);
// timetextBlock.Text = string.Format(“{0} hari{1} jam{2} detik”, timespan.Days, timespan.Hours, timespan.Seconds);
//cara memanggil
//timetextBlock.Text = relativeTimeAgo(timespan);
//membuktikan coding yang di buat dengan minute
TimeSpan posttime = DateTime.Now.Subtract(DateTime.Now.AddMinutes(-5));
timetextBlock.Text = relativeTimeAgo(posttime);
}
private string relativeTimeAgo(TimeSpan timespan)
{
//constanta nilai yang tidak bisa di ubah ubah
const int SECOND = 1;
const int MINUTE = 60 * SECOND;
const int HOUR = 60 * MINUTE;
const int DAY = 24 * HOUR;
const int MONTH = 30 * DAY;
double delta = Math.Abs(timespan.TotalSeconds);
if (delta < 0)
{
return "not yet";
}
if (delta < 1 * MINUTE)
{
return timespan.Seconds == 1 ? "one second ago " : timespan.Seconds + "seconds ago";
}
if (delta < 2 * MINUTE)
{
return "a minute ago";
}
if (delta < 45 * MINUTE)
{
return timespan.Minutes + "minutes ago";
}
if (delta < 90 * MINUTE)
{
return "an hour ago";
}
if (delta < 24 * HOUR)
{
return timespan.Hours + "hours ago";
}
if (delta < 48 * HOUR)
{
return "yesterday";
}
if (delta < 30 * DAY)
{
return timespan.Days + "days ago";
}
if (delta < 12 * MONTH)
{
int months = Convert.ToInt32(Math.Floor((double)timespan.Days / 30));
return months <= 1 ? "one month ago" : months + "months ago";
}
else
{
int years = Convert.ToInt32(Math.Floor((double)timespan.Days / 365));
return years <= 1 ? "one years ago" : years + "years ago";
}
}
}
}
• F5
• Output : beberapa tahun anda di lahir kan ,dan beberapa menit
cukup sekian pengenalan play with date and time part 1 kali ini
wassalamu’alaikum wr.wrb